
use "main_data.dta", clear
merge 1:1 UID using "survey_data.dta", nogen

{ // Defining variables, locals, other setup
local leftquote "\\\`"

local lightest "white"
local darker "gs11" 
local darkest "gs4"
local cap_color "black"

local C_color "`lightest'"
local F_color "`darker'"
local G6_color "`darkest'"
local G12_color "`darkest'"
local G18_color "`darkest'"
local L6_color "`darker'"
local L18_color "`darker'"
local G_color "`darkest'"
local L_color "`darker'"

local G_border "white"
local C_border "black"
local L_border "white"
local F_border "white"

local G6_border "`G6_color'"
local L6_border "`L6_color'"
local G12_border "`G12_color'"
local G18_border "`G18_color'"
local L18_border "`L18_color'"


gen got_grantbump6 = (treatment == 1)
gen got_loanbump6 = (treatment == 2)
gen got_grantbump12 = (treatment == 3)						
gen got_loanbump18 = (treatment == 4)
gen got_grantbump18 = (treatment == 5)						


gen grantbump_amount = 0
gen loanbump_amount = 0
replace grantbump_amount = 6 if got_grantbump6 == 1
replace grantbump_amount = 12 if got_grantbump12 == 1
replace grantbump_amount = 18 if got_grantbump18 == 1
replace loanbump_amount = 6 if got_loanbump6 == 1
replace loanbump_amount = 18 if got_loanbump18 == 1

gen anybump_amount = grantbump_amount + loanbump_amount

gen got_anybump = (treatment!=0)


gen year = cohort
replace year = 2016 if cohort == 2016.5

foreach t of numlist 0/2 {
	gen TFA_`t' = 100*inlist(employment_`t', 0) if !missing(employment_`t')
	gen teaching_`t' = 100*inlist(employment_`t', 0,1, 2) if !missing(employment_`t')
	gen teachingNotTFA_`t' = 100*inlist(employment_`t',1, 2) if !missing(employment_`t')
	gen forprofit_`t' = 100*inlist(1, working_1_1_`t', working_1_2_`t', working_1_3_`t', working_1_4_`t', working_1_6_`t', working_1_9_`t') if !missing(employment_`t')
	gen student_`t' = 100*inlist(employment_`t', 4) if !missing(employment_`t')
	gen publicservice_`t' = inlist(employment_`t', 0, 1, 2) | (working_1_5_`t' == 1) | (working_1_7_`t' == 1) | (working_1_8_`t' ==1) if !missing(employment_0_`t') | !missing(working_1_7_`t')
	replace publicservice_`t'= 100*publicservice_`t'
	gen self_publicservice_`t' = 100*(inlist(employment_`t', 0, 1, 2) | working_2_3_`t' == 1 | working_2_7_`t' == 1 | working_2_8_`t' == 1) if !missing(employment_`t') | !missing(working_2_3_`t')
	gen education_degree_`t' = 100*(inlist(1, student_1_1_`t')) if !missing(employment_`t')
	gen publicservice_degree_`t' = 100*(inlist(1, student_1_1_`t', student_1_4_`t')) if !missing(employment_`t')
	gen forprofit_degree_`t' = 100*(inlist(1, student_1_2_`t', student_1_3_`t', student_1_5_`t')) if !missing(employment_`t')
	gen poorer_school_`t' = 100*(inlist(employment_`t', 0) | inlist(1, teaching_2_1_`t', teaching_2_2_`t',  teaching_2_3_`t')) if !missing(employment_0)
	gen publicservice_noteach_`t' = 100*(publicservice_`t' == 100 & teaching_`t' == 0) if !missing(publicservice_`t')
	
}

foreach t of numlist 0/2 {
	cap drop salary_`t'
	gen salary_`t' = .
	foreach q in TFA_3 working_3 teaching_4 {
		cap replace salary_`t' = 15000 if `q'_1_`t' == 1
		cap replace salary_`t' = 30000 if `q'_2_`t' == 1
		cap replace salary_`t' = 50000 if `q'_3_`t' == 1
		cap replace salary_`t' = 70000 if `q'_4_`t' == 1
		cap replace salary_`t' = 90000 if `q'_5_`t' == 1
		cap replace salary_`t' = 125000 if `q'_6_`t' == 1
		cap replace salary_`t' = 175000 if `q'_7_`t' == 1
		cap replace salary_`t' = 250000 if `q'_8_`t' == 1
	}
	if `t' != 2 {
		sum salary_`t' if employment_`t' == 0
		replace salary_`t' = `r(mean)' if employment_`t' == 0 & missing(salary_`t')
	}
	cap drop salary_`t'_nonmiss
	gen salary_`t'_nonmiss = !missing(salary_`t')
}

gen in_survey_employment = 100*(in_survey == 3 & !missing(employment_0))
gen FDOS_Survey = FDOS if in_survey_employment == 100
egen credit_4 = rowmax(credit_1 credit_2 credit_3)

local got_GB6_lab "\\\$600 Grant"
local got_GB12_lab "\\\$1200 Grant"
local got_GB18_lab "\\\$1800 Grant"
local got_LB6_lab "\\\$600 Loan"
local got_LB18_lab "\\\$1800 Loan"

local AB_amount_lab "Extra Funding (\\\$100s)"
local GB_amount_lab "Extra Grant (\\\$100s)"
local LB_amount_lab "Extra Loan (\\\$100s)"
local cons_lab "Constant"

local AB_amountXec_dec2_10_b_lab "Extra Funding"
local AB_amountXec_dec2_10_se_lab "\multicolumn{1}{r|}{\$\times\$ 2nd`=uchar(8211)'10th Decile EC}"

local rightquote "'"

_pctile ec if year < 2017, percentiles(10 20 30 40 50 60 70 80 90)
gen ec_dec = 1 if ec <= `r(r1)'
replace ec_dec = 2 if ec > `r(r1)' & ec < `r(r2)'
replace ec_dec = 3 if ec >= `r(r2)' & ec <= `r(r3)'
replace ec_dec = 4 if ec > `r(r3)' & ec <= `r(r4)'
replace ec_dec = 5 if ec > `r(r4)' & ec <= `r(r5)'
replace ec_dec = 6 if ec > `r(r5)' & ec <= `r(r6)'
replace ec_dec = 7 if ec > `r(r6)' & ec <= `r(r7)'
replace ec_dec = 8 if ec > `r(r7)' & ec <= `r(r8)'
replace ec_dec = 9 if ec > `r(r8)' & ec <= `r(r9)'
replace ec_dec = 10 if ec > `r(r9)' & ec != .

replace ec = ec / 1000
rename (*grantbump* *loanbump* *anybump*) (*GB* *LB* *AB*)

label variable ec "Exp. Contribution"

label variable GrantAmount "TGL Grant"

label variable LoanAmount "TGL Loan"

tab ec_dec, gen(ec_dec)

gen ec_dec2_10 = inrange(ec_dec, 2, 10)
gen ec_dec3_10 = inrange(ec_dec, 3, 10)

foreach var in ec_dec3_10 {
	foreach F in A G L {
		gen `F'B_amountX`var' = `F'B_amount*`var'
		gen `F'B_amountXecX`var' = `F'B_amount*`var'*ec
	}
	gen ecX`var' = ec*`var'
}

local AB_amount_lab "Extra Funding (\\\$100s)"
local GB_amount_lab "Extra Grants (\\\$100s)"
local GB_amount_b_lab "Extra Grants (\\\$100s)"

local LB_amount_lab "Extra Loans (\\\$100s)"
local LB_amount_b_lab "Extra Loans (\\\$100s)"
local ec_lab "EC"

local AB_amount_int_lab "Extra Funding"
local GB_amount_int_lab "Extra Grants"
local LB_amount_int_lab "Extra Loans"
local got_GB6_int_lab "\\\$600 Grant"
local got_LB6_int_lab "\\\$600 Loan"
local got_GB12_int_lab "\\\$1200 Grant"
local got_GB18_int_lab "\\\$1800 Grant"
local got_LB18_int_lab "\\\$1800 Loan"

local AB_amount_lab_wUnit "Extra Funding (\\\$100s)"
local GB_amount_lab_wUnit "Extra Grants (\\\$100s)"
local GB_amount_b_lab_wUnit "Extra Grants (\\\$100s)"

local LB_amount_lab_wUnit "Extra Loans (\\\$100s)"
local LB_amount_b_lab_wUnit "Extra Loans (\\\$100s)"

local AB_amount_int_lab_wUnit "Extra Funding (\\\\$100s)"
local GB_amount_int_lab_wUnit "Extra Grants (\\\\$100s)"
local LB_amount_int_lab_wUnit "Extra Loans (\\\\$100s)"
local got_GB6_int_lab_wUnit "\\\$600 Grant"
local got_LB6_int_lab_wUnit "\\\$600 Loan"
local got_GB12_int_lab_wUnit "\\\$1200 Grant"
local got_GB18_int_lab_wUnit "\\\$1800 Grant"
local got_LB18_int_lab_wUnit "\\\$1800 Loan"

foreach var in 	AB_amount GB_amount 		///
				LB_amount got_GB6 got_LB6 got_GB12 got_GB18 got_LB18 ec {
	foreach n of numlist 1/10 {
		gen `var'Xec_dec`n' = `var'*ec_dec`n'
	}
	gen `var'Xec_dec2_10 = `var'*(ec_dec1 == 0)
	
local `var'_lab "``var'_int_lab_wUnit'"
local `var'_b_lab "``var'_int_lab_wUnit'"
local `var'Xec_dec1_lab "``var'_int_lab' \$\times\$ 1st Decile EC"
local `var'Xec_dec1_b_lab "``var'_int_lab_wUnit'"
local `var'Xec_dec1_se_lab "\hspace{15mm}\$\times\$ 1st Decile EC"

local `var'Xec_dec2_10_lab "``var'_int_lab' \$\times\$ 2nd`=uchar(8211)'10th Decile EC"
local `var'Xec_dec2_10_b_lab "``var'_int_lab_wUnit'"
local `var'Xec_dec2_10_se_lab "\multicolumn{1}{r|}{ \$\times\$ 2nd`=uchar(8211)'10th Decile EC}"

local `var'Xec_dec2_lab "``var'_int_lab' \$\times\$ 2nd Decile EC"
local `var'Xec_dec2_b_lab "``var'_int_lab'"
local `var'Xec_dec2_se_lab "\multicolumn{1}{r|}{ \$\times\$ 2nd Decile EC}"

local `var'Xec_dec3_lab "``var'_int_lab' \$\times\$ 3rd Decile EC"
local `var'Xec_dec3_b_lab "``var'_int_lab'"
local `var'Xec_dec3_se_lab "\multicolumn{1}{r|}{ \$\times\$ 3rd Decile EC}"

local `var'Xec_dec4_lab "``var'_int_lab' \$\times\$ 4th Decile EC"
local `var'Xec_dec4_b_lab "``var'_int_lab'"
local `var'Xec_dec4_se_lab "\multicolumn{1}{r|}{ \$\times\$ 4th Decile EC}"

local `var'Xec_dec5_lab "``var'_int_lab' \$\times\$ 5th Decile EC"
local `var'Xec_dec5_b_lab "``var'_int_lab'"
local `var'Xec_dec5_se_lab "\multicolumn{1}{r|}{ \$\times\$ 5th Decile EC}"

local `var'Xec_dec6_lab "``var'_int_lab' \$\times\$ 6th Decile EC"
local `var'Xec_dec6_b_lab "``var'_int_lab'"
local `var'Xec_dec6_se_lab "\multicolumn{1}{r|}{ \$\times\$ 6th Decile EC}"

local `var'Xec_dec7_lab "``var'_int_lab' \$\times\$ 7th Decile EC"
local `var'Xec_dec7_b_lab "``var'_int_lab'"
local `var'Xec_dec7_se_lab "\multicolumn{1}{r|}{ \$\times\$ 7th Decile EC}"

local `var'Xec_dec8_lab "``var'_int_lab' \$\times\$ 8th Decile EC"
local `var'Xec_dec8_b_lab "``var'_int_lab'"
local `var'Xec_dec8_se_lab "\multicolumn{1}{r|}{ \$\times\$ 8th Decile EC}"

local `var'Xec_dec9_lab "``var'_int_lab' \$\times\$ 9th Decile EC"
local `var'Xec_dec9_b_lab "``var'_int_lab'"
local `var'Xec_dec9_se_lab "\multicolumn{1}{r|}{ \$\times\$ 9th Decile EC}"

local `var'Xec_dec10_lab "``var'_int_lab' \$\times\$ 10th Decile EC"
local `var'Xec_dec10_b_lab "``var'_int_lab'"
local `var'Xec_dec10_se_lab "\multicolumn{1}{r|}{ \$\times\$ 10th Decile EC}"
}


local ec_dec1_lab "1st Decile EC"
local ec_dec2_lab "2nd Decile EC"
local ec_dec3_lab "3rd Decile EC"
local ec_dec4_lab "4th Decile EC"
local ec_dec5_lab "5th Decile EC"
local ec_dec6_lab "6th Decile EC"
local ec_dec7_lab "7th Decile EC"
local ec_dec8_lab "8th Decile EC"
local ec_dec9_lab "9th Decile EC"
local ec_dec10_lab "10th Decile EC"
local ec_dec1_b_lab "1st Decile EC"
local ec_dec2_b_lab "2nd Decile EC"
local ec_dec3_b_lab "3rd Decile EC"
local ec_dec4_b_lab "4th Decile EC"
local ec_dec5_b_lab "5th Decile EC"
local ec_dec6_b_lab "6th Decile EC"
local ec_dec7_b_lab "7th Decile EC"
local ec_dec8_b_lab "8th Decile EC"
local ec_dec9_b_lab "9th Decile EC"
local ec_dec10_b_lab "10th Decile EC"

local AB_amount_b_lab "Extra Funding (\\\$100s)"
local cons_b_lab "Constant"

gen grantbump_amount = 0
replace grantbump_amount = 600 if got_GB6 == 1
replace grantbump_amount = 1200 if got_GB12 == 1
replace grantbump_amount = 1800 if got_GB18 == 1

gen loanbump_amount = 0
replace loanbump_amount = 600 if got_LB6 == 1
replace loanbump_amount = 1800 if got_LB18 == 1

gen ControlGrant = (GrantAmount*100 - grantbump_amount)
gen ControlLoan = (LoanAmount*100 - loanbump_amount)
gen ControlAward = ControlGrant + ControlLoan

foreach d of numlist 1/10 {
	gen got_GB6_ec`d' = got_GB6 * ec_dec`d'
	gen got_LB6_ec`d' = got_LB6 * ec_dec`d'
	gen got_GB12_ec`d' = got_GB12 * ec_dec`d'
	gen got_GB18_ec`d' = got_GB18 * ec_dec`d'
	gen got_LB18_ec`d' = got_GB18 * ec_dec`d'
}
label var got_GB6 "\\$600 Grant"
label var got_LB6 "\\$600 Loan"
label var got_GB12 "\\$1,200 Grant"
label var got_GB18 "\\$1,800 Grant"
label var got_LB18 "\\$1,800 Loan"


gen apply_for_loan = 100*inlist(1, credit_1, credit_2, credit_3) if !missing(credit_0)
	gen high_incentive = (incentive_group==2 & ec_dec1==1) | (incentive_group==1 & ec_dec1==0)
	gen incentive_groupXEC1 = incentive_group*ec_dec1
	gen incentive_groupXEC2_10 = (3-incentive_group)*(ec_dec1==0)
	
	local teaching_0_lab "Teaching 0 years out"
	local teaching_1_lab "Teaching 2 years out"
	local teaching_2_lab "Teaching 10 years out"
	local publicservice_0_lab "Public Service 0 years out"
	local publicservice_1_lab "Public Service 2 years out"
	local publicservice_2_lab "Public Service 10 years out"
	local forprofit_0_lab "Private sector 0 years out"
	local forprofit_1_lab "Private sector 2 years out"
	local forprofit_2_lab "Private sector 10 years out"
	local student_0_lab "Student 0 years out"
	local student_1_lab "Student 2 years out"
	local high_incentive_lab "High Incentive"
	local salary_0_lab "Salary 0 years out"
	local salary_1_lab "Salary 2 years out"
	local salary_2_lab "Salary 10 years out"
	local FDOS_lab "TFA FDOS"
	

local d1_title "1st"
local d2_title "2nd"
local d3_title "3rd"
local d4_title "4th"
local d5_title "5th"
local d6_title "6th"
local d7_title "7th"
local d8_title "8th"
local d9_title "9th"
local d10_title "10th"
tab ec_dec, gen(ec_dec_)

recode treatment (0=0) (1=2) (2=1) (3=3) (4=5) (5=4)
label def treatment_recode 0 "C" 1 "L6" 2 "G6" 3 "G12" 4 "G18" 5 "L18"
label values treatment treatment_recode

cap drop LeftAnyOnTable_Grants
gen LeftAnyOnTable_Grants = 100*(AcceptedGrant  < GrantAmount*100 - 10) 			///
			if !missing(AcceptedGrant) & !missing(GrantAmount)
cap drop LeftAnyOnTable_Loans
gen LeftAnyOnTable_Loans = 100*(AcceptedLoan < LoanAmount*100 - 10) 			///
			if !missing(AcceptedLoan) & !missing(LoanAmount)

gen duration_in_minutes = durationinseconds / 60


}

***** TABLE 1: Treatment Assignments
{
preserve
replace cohort = 20162 if cohort==2016.5
cap file close tex
file open tex using "Table1.tex", write replace
file write tex "\begin{tabular}{l@{\hskip 1em}cccc@{\hskip 3em}c}"_n
file write tex "\toprule" _n
file write tex "& \multirow{2}{*}{2015} & 2016 & 2016 & \multirow{2}{*}{2017} & \multirow{2}{*}{Total}  \\" _n
file write tex "&                       & (1st half) & (2nd half) &  &   \\ " _n
file write tex "\midrule" _n
file write tex "\addlinespace[2mm]" _n

local status1 "1st Decile of EC" 
local status2 "2nd Decile" 
local status3 "3rd--10th Deciles"

gen status = .
replace status = 1 if ec_dec == 1
replace status = 2 if inlist(ec_dec, 2)
replace status = 3 if inrange(ec_dec, 3,10)

tempfile temp
save `temp'
replace cohort = 9999
append using `temp'

local ts1 0 1 2 3
local ts2 0 1 2 3
local ts3 0 1 2 3 5 4


local lab_0 "Control"
local lab_1 "\\\$600 Loan"
local lab_2 "\\\$600 Grant" 
local lab_3 "\\\$1200 Grant"
local lab_4 "\\\$1800 Grant"
local lab_5 "\\\$1800 Loan"

foreach status in 1 2 3 {
    if (`status'!=1){
        file write tex "\addlinespace[3mm]" _n
    }
    file write tex "{\sc `status`status''} \\" _n
    file write tex "\addlinespace[0.5mm]" _n
    foreach treatment in `ts`status'' {
        file write tex "`lab_`treatment''"
       foreach cohort in 2015 2016 20162 2017 9999 {
            qui count if treatment == `treatment' & cohort == `cohort' & status == `status'
            if `r(N)' != 0 {
                file write tex " & `r(N)'"
            }
            else {
                file write tex " &"
            }
        }
        file write tex " \\" _n
    }
    
}
file write tex "\bottomrule" _n
file write tex "\end{tabular}"
file close tex
restore
}

***** TABLE 3: Summary Statistics. NOTE that this does not run because it relies on demographics, which we cannot share.
{
/*
gen RegionalCost = RegionalCostLocal
replace RegionalCost = RegionalCost + 150 if local == 0

foreach status in 1 2 3 4 {
preserve

global zero_to_hundreds Senior female white  Pell  local  RegionNotFirstChoice SubjectNotFirstChoice
global others FIT Age ec Income CheckingSavings CreditCard FederalLoans POL ///
			 ParentalContribution  Dependents RegionalCost FederalLoans 

foreach var of global zero_to_hundreds {
replace `var' = `var'*100 
}
replace GrantAmount = GrantAmount*100
replace LoanAmount = LoanAmount*100
replace Income = Income * 10000
replace CreditCard = CreditCard * 10000
replace ec = ec * 1000
replace FederalLoans = FederalLoans*10000
replace POL = POL*10000
replace OtherLoans = OtherLoans*10000

if `status' == 1 keep if ec_dec == 1
if `status' == 2 keep if inrange(ec_dec, 2, 2)
if `status' == 3 keep if inrange(ec_dec, 3, 10)
if `status' == 4 keep if inrange(ec_dec, 1, 10)
foreach var in ${zero_to_hundreds} Age {
qui sum `var' if `var' != -1
local `var'_s`status': display %3.1f `r(mean)'
}
foreach var of global others {
if "`var'" == "Age" continue
qui sum `var' if `var' != -1
if inlist("`var'", "Dependents", "FIT") {
local `var'_s`status': display %3.2f `r(mean)'

}
else {
local `var'_s`status': display %12.0fc `r(mean)'
}
}
qui count
local N_s`status': display %12.0fc `r(N)'

restore
}
local indent_space "5mm"
local treat0_lab "Control"
local treat1_lab "Grant $600"
local treat2_lab "Loan $600"
local treat3_lab "Grant $1,200"
local treat4_lab "Loan $1,800"
local treat5_lab "Grant $1,800"

local local_lab "Local (\%)"
local Senior_lab "\hspace{`indent_space'} Graduating Senior (\%)"
local female_lab "Female (\%)"
local white_lab "White (\%)"
local FIT_lab = subinstr("`leftquote'`leftquote'", "\", "", 1) + "Fit" + "\`rightquote'\`rightquote'" + " Score"

local ec_lab "Expected Contribution (\\\$)"
local Income_lab "\hspace{`indent_space'} Income (\\\$)"
local Dependents_lab "\hspace{`indent_space'} Number of Dependents"
local CreditCard_lab "\hspace{`indent_space'} Credit Card Debt (\\\$)"
local FederalLoans_lab "Federal Loans (\\\$)"
local PrivateLoans_lab "\hspace{`indent_space'} Private Student Loans (\\\$)"
local POL_lab "\hspace{`indent_space'} Private Student Loans (\\\$)"
local OtherLoans_lab "\hspace{`indent_space'} Other Student Loans (\\\$)"
local ParentalContribution_lab "\hspace{`indent_space'} Parental Contribution (\\\$)"
local PC_lab "\hspace{`indent_space'} Parental Contribution (\\\$)"
local Age_lab "Age"
local RegionNotFirstChoice_lab "Region Not First Choice (\%)"
local RNFC_lab "Region Not First Choice (\%)"
local SubjectNotFirstChoice_lab "Subject Not First Choice (\%)"
local SNFC_lab "Subject Not First Choice (\%)"
local RegionalCost_lab "Regional Cost (\\\$)"
local RC_lab "Regional Cost (\\\$)"
local CheckingSavings_lab "\hspace{`indent_space'} Checking and Savings (\\\$)"

file open tex using "Table3.tex", write replace

file write tex "\begin{tabular}{lccc@{\hskip 0.75em}c}" _n
file write tex "\toprule" _n
file write tex "& \multirow{3}{*}{Full sample} & \multicolumn{3}{c}{\multirow{2}{*}{\shortstack{By Decile of \\ Expected Contribution}}} \\ " _n
file write tex "&  &     &     &           \\ " _n
file write tex "&  & 1st & 2nd & 3rd--10th \\ " _n
file write tex "\midrule" _n

foreach var of varlist  female white Age FIT RegionNotFirstChoice SubjectNotFirstChoice ec CheckingSavings ParentalContribution Income CreditCard 	///
			POL Senior Dependents local RegionalCost FederalLoans {
    if inlist("`var'","ec","local") {
        file write tex "\addlinespace[2mm]" _n
    }
    file write tex `"``var'_lab'"'
    foreach status in 4 1 2 3 {
        file write tex " &  ``var'_s`status''"
    }
    file write tex " \\" _n
}
file write tex "\addlinespace[2mm]" _n
file write tex  "\$N\$"

foreach status in 4 1 2 3 {
    file write tex " & `N_s`status''"
}
file write tex " \\ " _n
file write tex "\bottomrule" _n 
file write tex "\end{tabular}"

file close tex


*/
}
***** TABLE 4: Treatment Effects of Additional Grants and Loans
{
local depvar FDOS
local regs 1 3 5 7 9 11
local xvars1 "GB_amount LB_amount"				
local xvars2 	GB_amountXec_dec1 LB_amountXec_dec1 						///
				GB_amountXec_dec2 LB_amountXec_dec2 					///
				GB_amountXec_dec3 LB_amountXec_dec3 					///
				GB_amountXec_dec4 LB_amountXec_dec4 					///
				GB_amountXec_dec5 LB_amountXec_dec5 					///
				GB_amountXec_dec6 LB_amountXec_dec6 					///
				GB_amountXec_dec7 LB_amountXec_dec7 					///
				GB_amountXec_dec8 LB_amountXec_dec8 					///
				GB_amountXec_dec9 LB_amountXec_dec9 					///
				GB_amountXec_dec10 LB_amountXec_dec10 					///
				ec_dec1 ec_dec2 ec_dec3 ec_dec4 ec_dec5					///			
				ec_dec6 ec_dec7 ec_dec8 ec_dec9 
local depvar FDOS			
qui {
foreach r in `regs' {
	if inlist(`r', 3, 4, 7, 8, 11, 12) {
		local num_pos_sig_`r' = 0
		local num_neg_sig_`r' = 0
	}
	else {
		local num_pos_sig_`r' = ""
		local num_neg_sig_`r' = ""
	}
	noisily display `r'
	if inlist(`r', 1, 2, 3, 4) {
		local cond "year != 2017"
	}
	if inlist(`r', 5, 6, 7, 8) {
		local cond "year == 2017"
	}
	if inlist(`r', 9, 10, 11, 12) {
		local cond "year != 0"
	}
	if inlist(`r', 1, 3, 5, 7, 9, 11) {
		local extra_vars ""		
	}
	if inlist(`r', 2, 4, 6, 8, 10, 12) {
		local extra_vars "${demos}"		
	}
	if inlist(`r', 1, 2, 5, 6, 9, 10) {
		local xvars `xvars1'
	}
	if inlist(`r', 3, 4, 7, 8, 11, 12) {
		local xvars `xvars2'
	}
	xi: areg `depvar' `xvars'  `extra_vars' if `cond', 	///
									absorb(Batch) r
	matrix define A = r(table)
	local N_`r': display %3.0f `e(N)'
	local r2_`r': display %3.2f `e(r2)'
	qui sum `depvar' if e(sample) == 1 & treatment==0
	local ymean_`r': display %3.2f `r(mean)'		
	qui sum `depvar' if e(sample) == 1 & treatment==0 & ec_dec == 1
	local ymean_EC1_`r': display %3.2f `r(mean)'
	local i = 0
	foreach var in `xvars' cons {
		local i = `i' + 1
		if "`var'" == "cons" local i = colsof(A)
		local b_`var'_`r': display %3.2f A[1, `i']
		local se_`var'_`r': display %3.2f A[2, `i']
		local p_`var'_`r' = A[4, `i']
		local s_`var'_`r' = ""
		if inlist(`r', 3, 4, 7, 8, 11, 12) {
			if 	`p_`var'_`r'' < .1 &								///
				strpos("`var'", "B_amountX") != 0 & 				///
				strpos("`var'", "ec_dec1") == 0					///
			{
				if `b_`var'_`r'' > 0 {
					local num_pos_sig_`r' = `num_pos_sig_`r'' + 1
				}
				if `b_`var'_`r'' < 0 {
					local num_neg_sig_`r' = `num_neg_sig_`r'' + 1
				}
			}
		}
		if `p_`var'_`r'' < .1 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
		if `p_`var'_`r'' < .05 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
		if `p_`var'_`r'' < .01 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
	}	
}
}
local regs 1 2 3 4 5 6 7 8 9 10 11 12

local xvars		GB_amount LB_amount 										///
				GB_amountXec_dec1 LB_amountXec_dec1 					
				
file open tex using "Table4.tex", write replace

file write tex "\begin{tabular}{lcccc@{\hskip 3em}cccc@{\hskip 3em}cccc}" _n
file write tex "\toprule"_n
file write tex "\addlinespace[1mm]"_n
file write tex "& \multicolumn{4}{c}{2015--2016} & \multicolumn{4}{c}{2017} & \multicolumn{4}{c}{2015--2017} \\" _n
file write tex "\addlinespace[1mm]"_n
file write tex "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) & (11) & (12) \\" _n
file write tex "\midrule" _n
foreach var in `xvars'  {
	file write tex  "``var'_b_lab'"
	foreach r in `regs' {
		file write tex "& `b_`var'_`r''`s_`var'_`r''"
	}
	file write tex " \\" _n
	file write tex  "``var'_se_lab'"
	foreach r in `regs' {
		if "`se_`var'_`r''" != "" {
			file write tex `"& (`se_`var'_`r'')"'
		}
		else {
			file write tex `"& "'
		}
	}
	file write tex " \\" _n
}
file write tex "Demographics & No & Yes & No & Yes & No & Yes & No & Yes & No & Yes & No & Yes"
file write tex " \\" _n "Batch FEs "
foreach r in `regs' {
		file write tex " & Yes "
}
file write tex " \\" _n 
file write tex "\addlinespace[5mm]" _n
file write tex "\$N\$"
foreach r in `regs' {
	file write tex " & `N_`r''"
}

file write tex " \\" _n "\$R^2\$"
foreach r in `regs' {
		file write tex " & `r2_`r''"
}
file write tex " \\" _n
file write tex "\addlinespace[5mm]" _n

file write tex "Control Mean"
foreach r in `regs' {
	file write tex " & `ymean_`r''"
}
file write tex " \\" _n "1st Decile Control Mean"
foreach r in `regs' {
	file write tex " & `ymean_EC1_`r''"
}
file write tex " \\ " _n 
file write tex "\addlinespace[5mm]" _n
file write tex "\multicolumn{13}{l}{\itshape Number of interactions with other deciles\ldots} \\" _n
file write tex "\addlinespace[1mm]" _n
		file write tex "\ldots that are positive, \$p < 0.10\$ "
		foreach r in `regs' {
				file write tex " & `num_pos_sig_`r'' "
		}
		file write tex " \\" _n "\ldots that are negative,  \$p < 0.10\$ "
		foreach r in `regs' {
				file write tex " & `num_neg_sig_`r'' "
		}
file write tex " \\ " _n
file write tex "\bottomrule" _n 
file write tex "\end{tabular}"
file close tex
include clear_locals.do 

}

***** TABLE 5: Liquidity Need and Credit Access
{
	preserve
	gen one = 1
	cap drop credit_5
	gen credit_5 = 0 if credit_0 == 2 & !missing(credit_1)
	replace credit_5= 1 if inlist(1, 	credit_5_2, credit_7_2, credit_9_2, ///
											credit_5_3, credit_7_3, credit_9_3)

	gen credit_6 = 0 if credit_0 == 2 & !missing(credit_1)
	replace credit_6= 1 if inlist(1, 	credit_5_1, credit_7_1, credit_9_1)

	replace credit_0 = credit_0 == 2 if !missing(credit_0)
	
	sum credit_0 if ec_dec == 1 & treatment == 0
	
	gen AnyDiscourage = 0 if credit_0 == 1 & !missing(credit_1)
	replace AnyDiscourage = 100 if inlist(1, credit_4_4, credit_6_4, credit_8_3)
	gen AnyDenial = 100*credit_6
	
	gen AnyDenDisc = 100*(AnyDiscourage == 100 | AnyDenial == 100) if !missing(credit_1)
	gen credit_90 = AnyDiscourage/100
	gen credit_91 = AnyDenDisc/100
	gen no_credit = 100*(credit_5 == 0 & AnyDenDisc==100) if !missing(credit_1)
	gen credit_92 = no_credit/100

	gen SoughtAny = credit_4
	gen AnyDenialIfSought = credit_6 if credit_4 == 1
	gen NumberApplications = credit_1 + credit_2 + credit_3
	
	gen NumDenials = (credit_5_1 == 1) + (credit_7_1 == 1) + (credit_9_1 == 1) if !missing(credit_1)
	gen NumDenialsDiscourage = (credit_5_1 == 1) + (credit_7_1 == 1) + (credit_9_1 == 1) + (credit_4_4 == 1) + (credit_6_4 == 1) + (credit_8_3 == 1) if !missing(credit_1)
	gen Unable = inlist(1, credit_4_1, credit_4_4, credit_6_1, credit_6_4, credit_8_1, credit_8_3) if !missing(credit_0)
	gen DontKnowDidntOccur = inlist(1, credit_4_2==1, credit_4_3, credit_6_2, credit_6_3, credit_8_2)
	gen CoveredAnotherWay = inlist(1, credit_4_5, credit_6_5, credit_8_4)
		
	tab Unable if SoughtAny == 0 & ec_dec==1 & CoveredAnotherWay == 0
	tab DontKnowDidntOccur if SoughtAny == 0 & ec_dec==1 & CoveredAnotherWay == 0
		
	tab Unable if SoughtAny == 0 & ec_dec!=1 & CoveredAnotherWay == 0
	tab DontKnowDidntOccur if SoughtAny == 0 & ec_dec!=1 & CoveredAnotherWay == 0
	gen SoughtFormalLoan = credit_1 == 1 | credit_2 == 1 if !missing(credit_1) & !missing(credit_2)
	gen DeniedFormalLoan = 0 if !missing(credit_1)
	replace DeniedFormalLoan= 1 if inlist(1, 	credit_5_1, credit_7_1)

	local indent_space "5mm"

	local credit_0_lab "Needed additional funds"
	local credit_1_lab "\hspace{`indent_space'} Applied for credit card"
	local credit_2_lab "\hspace{`indent_space'} Applied for bank loan"
	local credit_3_lab "\hspace{`indent_space'} Sought informal loan or gift"
	local credit_4_lab "Sought any funding"
	local credit_5_lab "Received any funding"
	local credit_6_lab "Any denial"
	local credit_90_lab "Any discouragement"
	local credit_91_lab "Any discouragement or denial"
	local credit_92_lab "No credit access"
	local EC1_1_lab "1st"
	local EC1_0_lab "2nd--10th"

	foreach n of numlist 0 4 1 2 3 5 6 90/92 {
		foreach d of numlist 0/1 {
			qui sum credit_`n' if ec_dec1 == `d' & treatment == 0
			local credit_`n'_EC1_`d': display %3.1f 100*`r(mean)'
			local N_`n'_EC1_`d' = `r(N)'
		}
	}
	foreach n of numlist 0 4 1 2 3 5 6 90/92 {
		foreach d of numlist 0/1 {
			qui sum credit_`n' if ec_dec1 == `d' & treatment != 0
			local credit_`n'_EC1_`d'_all: display %3.1f 100*`r(mean)'
			local N_`n'_EC1_`d'_all = `r(N)'
		}
	}

	file open table using "Table5.tex", write replace
	
    file write table "\begin{tabular}{l@{\hskip 2.5em}cc@{\hskip 2em}cc}"_n
    file write table "\toprule" _n
	file write table "& \multicolumn{4}{c}{Decile of Expected Contribution} \\" _n
	file write table "& \multicolumn{2}{c}{\hskip -2em `EC1_1_lab'}& \multicolumn{2}{c}{`EC1_0_lab'} \\" _n
	file write table "& Control & Treatment & Control & Treatment \\" _n
	file write table "\midrule" _n
	foreach n of numlist 0 {
		file write table "`credit_`n'_lab'"
		foreach d of numlist 1/0 {
			foreach suf in "" "_all" {
				file write table "& `credit_`n'_EC1_`d'`suf''\%"
			}
		}
		file write table " \\" _n
	}
    file write table "\addlinespace[2mm]" _n
	file write table "\$N\$ & `N_0_EC1_1' & `N_0_EC1_1_all' & `N_0_EC1_0' & `N_0_EC1_0_all' \\" _n
    file write table "\addlinespace[2mm]" _n
	file write table "\multicolumn{5}{c}{\sc Conditional on Needing Additional Funds} \\" _n
    file write table "\addlinespace[1mm]" _n
	foreach n of numlist 4 1 2 3 5 6 90/92 {
		if ("`credit_`n'_lab'"=="Received any funding") {
            file write table "\addlinespace[2mm]" _n
        }
        file write table "`credit_`n'_lab'"
		foreach d of numlist 1/0 {
			foreach suf in "" "_all" {
				file write table "& `credit_`n'_EC1_`d'`suf''\%"
			}
		}
		file write table " \\" _n
	}
    file write table "\addlinespace[2mm]" _n
	file write table "\$N\$ & `N_1_EC1_1' & `N_1_EC1_1_all' & `N_1_EC1_0' & `N_1_EC1_0_all' \\" _n
    file write table "\bottomrule" _n
	file write table "\end{tabular}"
	
    file close table
	include clear_locals.do 

	restore
}

***** TABLE 6: Treatment Effects on Actual and Expected Occupations. NOTE that this does not run because it relies on demographics, which we cannot share.
{
/*
local depvar_1 FDOS
local depvar_2 teaching_0
local depvar_3 teaching_1
local depvar_4 forprofit_0
local depvar_5 forprofit_1
local depvar_6 student_0
local depvar_7 student_1

local cond "!missing(employment_0)"
local xvars AB_amountXec_dec1 AB_amountXec_dec2 AB_amountXec_dec3 AB_amountXec_dec4 AB_amountXec_dec5 AB_amountXec_dec6 AB_amountXec_dec7 AB_amountXec_dec8 AB_amountXec_dec9 AB_amountXec_dec10 ec_dec1 ec_dec2 ec_dec3 ec_dec4 ec_dec5 ec_dec6 ec_dec7 ec_dec8 ec_dec9
local regs 1 2 3 4 5 6 7

foreach r in `regs' {
	local num_pos_sig_`r' = 0
	local num_neg_sig_`r' = 0	
	local extra_vars "${demos} "
	qui xi: areg `depvar_`r'' `xvars'  `extra_vars'   if `cond', 	///
									absorb(AwardsDate) r
	matrix define A = r(table)
	local N_`r': display %3.0f `e(N)'
	local r2_`r': display %3.2f `e(r2)'
	qui sum `depvar_`r'' if e(sample) == 1
	local ymean_`r': display %3.2f `r(mean)'		
	local i = 0
	foreach var in `xvars'   cons {
		local i = `i' + 1
		if "`var'" == "cons" local i = colsof(A)
		local b_`var'_`r': display %3.2f A[1, `i']
		local se_`var'_`r': display %3.2f A[2, `i']
		local p_`var'_`r' = A[4, `i']
		local s_`var'_`r' = ""
		if 	`p_`var'_`r'' < .1 & 								///
			strpos("`var'", "AB_amountX") != 0 & 				///
			!inlist("`var'", "AB_amountXec_dec1") ///
		{
			if `b_`var'_`r'' > 0 {
				local num_pos_sig_`r' = `num_pos_sig_`r'' + 1
			}
			if `b_`var'_`r'' < 0 {
				local num_neg_sig_`r' = `num_neg_sig_`r'' + 1
			}
		}
		if `p_`var'_`r'' < .1 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
		if `p_`var'_`r'' < .05 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
		if `p_`var'_`r'' < .01 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
	}	
}

local xvars AB_amountXec_dec1

file open tex using "Table6.tex", write replace
foreach var in `xvars'   {
	file write tex  "``var'_b_lab'"
	foreach r in `regs' {
		file write tex "& `b_`var'_`r''`s_`var'_`r''"
	}
	file write tex " \\" _n
	file write tex  "``var'_se_lab'"
	foreach r in `regs' {
		if "`se_`var'_`r''" != "" {
			file write tex `"& (`se_`var'_`r'')"'
		}
		else {
			file write tex `"& "'
		}
	}
	file write tex " \\" _n
}

file write tex "\addlinespace[1mm]" _n
file write tex /*" \\" _n*/ "\$N\$"
foreach r in `regs' {
	file write tex " & `N_`r''"
}

file write tex " \\" _n "\$R^2\$"
foreach r in `regs' {
		file write tex " & `r2_`r''"
}
file write tex "\\" _n
file write tex "\addlinespace[1mm]" _n
file write tex "Mean of Dependent Variable"
foreach r in `regs' {
	file write tex " & `ymean_`r''"
}
file write tex "\\" _n
file write tex "\addlinespace[3mm]" _n
file write tex "\multicolumn{8}{l}{\itshape Number of interactions with other deciles that are\ldots} \\" _n
file write tex "\addlinespace[1mm]" _n
file write tex "\ldots positive, \$p < 0.10\$ "
foreach r in `regs' {
		file write tex " & `num_pos_sig_`r'' "
}
file write tex " \\" _n "\ldots negative, \$p < 0.10\$ "
foreach r in `regs' {
		file write tex " & `num_neg_sig_`r'' "
}
file write tex " \\" _n
file write tex " \addlinespace[1mm]" _n
file write tex " \bottomrule"

file close tex

include clear_locals.do


*/
}

***** TABLE A2: Differential Financial Incentives for Survey Completion
{
	file open table using "TableA2.tex", write replace
	foreach d of numlist 1/0 {
		foreach h of numlist 0/1 {
			count if ec_dec1 == `d' & high_incentive == `h'
			local N_EC1`d'_HI`h' = `r(N)'
			sum in_survey if ec_dec1 == `d' & high_incentive == `h'
			local rate_EC1`d'_HI`h': display %3.1f `r(mean)'
		}
	}
	file write table "Certain \\$20 & `N_EC11_HI0' & `rate_EC11_HI0'\% & 0 & --- \\" _n
	file write table "Certain \\$40 & `N_EC11_HI1' & `rate_EC11_HI1'\% & 0 & --- \\" _n
	file write table "0.5\% chance at \\$500 & 0 & --- & `N_EC10_HI0' & `rate_EC10_HI0'\% \\" _n
	file write table "1\% chance at \\$500 & 0 & --- & `N_EC10_HI1' & `rate_EC10_HI1'\% \\" _n
	
	file close table
}

***** TABLE A3: Selection into Survey on Demographics. NOTE that this does not run because it relies on demographics, which we cannot share.
{
/*
	preserve
	gen RCNL = RegionalCostNonLocal 
	gen one = 1
	global zero_to_hundreds  female white    RNFC SNFC
	global others FIT Age ec 
	foreach var of global zero_to_hundreds {
		replace `var' = `var'*100 
	}
	replace GrantAmount = GrantAmount*100
	replace LoanAmount = LoanAmount*100
	replace Income = Income * 10000
	replace CreditCard = CreditCard * 10000
	replace ec = ec * 1000
	replace FederalLoans = FederalLoans*10000
	replace POL = POL*10000
	replace OtherLoans = OtherLoans*10000
	replace PC = PC*10000
	foreach ec1 in 0 1 {
		foreach h in 0 1 {
			foreach var of global zero_to_hundreds {
				qui sum `var' if ec_dec1 == `ec1' & high_incentive==`h' & in_survey == 100 & `var' != -1
				local `var'_ec1`ec1'_h`h': display %3.1f `r(mean)'
			}
			foreach var of global others {
				qui sum `var' if ec_dec1 == `ec1' & high_incentive==`h' & in_survey == 100 & `var' != -1
				if inlist("`var'", "Dependents", "FIT", "Age") {
					local `var'_ec1`ec1'_h`h': display %3.2f `r(mean)'
				}
				else {
					local `var'_ec1`ec1'_h`h': display %12.0fc `r(mean)'
				}
			}
			qui count if ec_dec1 == `ec1' & high_incentive==`h' & in_survey == 100
			local N_ec1`ec1'_h`h': display %12.0fc `r(N)'	
		}

		foreach var of varlist ${others} {
			mean `var'  if ec_dec1 == `ec1' & in_survey == 100 & `var' != -1, over(high_incentive one)
			test _subpop_1 = _subpop_2
			local p_`var'_ec1`ec1'_h1: display %3.2f `r(p)'
			local s_`var'_ec1`ec1'_h1 = ""
			if `p_`var'_ec1`ec1'_h1' < .1 local s_`var'_ec1`ec1'_h1 = "*"
			if `p_`var'_ec1`ec1'_h1' < .05 local s_`var'_ec1`ec1'_h1 = "**"
			if `p_`var'_ec1`ec1'_h1' < .01 local s_`var'_ec1`ec1'_h1 = "***"
		}
		foreach var of varlist ${zero_to_hundreds} {
			cap drop temp_var
			gen temp_var = `var'/100
			prtest temp_var if ec_dec1 == `ec1' & in_survey == 100 & !inlist(`var', -1, -100), by(high_incentive)
			local p_`var'_ec1`ec1'_h1: display %3.2f `r(p)'
			local s_`var'_ec1`ec1'_h1 = ""
			if `p_`var'_ec1`ec1'_h1' < .1 local s_`var'_ec1`ec1'_h1 = "*"
			if `p_`var'_ec1`ec1'_h1' < .05 local s_`var'_ec1`ec1'_h1 = "**"
			if `p_`var'_ec1`ec1'_h1' < .01 local s_`var'_ec1`ec1'_h1 = "***"
		}

		
		foreach s in 0 100 {
			foreach var of global zero_to_hundreds {
				qui sum `var' if ec_dec1 == `ec1' & in_survey==`s' & !inlist(`var', -1, -100),
				local `var'_ec1`ec1'_s`s': display %3.1f `r(mean)'
				local se_`var'_ec1`ec1'_s`s': display %3.1f `r(sd)'/(`r(N)'^(1/2))
			}
			foreach var of global others {
				qui sum `var' if ec_dec1 == `ec1' & in_survey==`s' & `var' != -1
				if inlist("`var'", "Dependents", "FIT", "Age") {
					local `var'_ec1`ec1'_s`s': display %3.1f `r(mean)'
					local se_`var'_ec1`ec1'_s`s': display %3.1f `r(sd)'/(`r(N)'^(1/2))
				}
				else {
					local `var'_ec1`ec1'_s`s': display %12.0fc `r(mean)'
					local se_`var'_ec1`ec1'_s`s': display %12.0fc `r(sd)'/(`r(N)'^(1/2))
				}
			}
			qui count if ec_dec1 == `ec1' & in_survey==`s'
			local N_ec1`ec1'_s`s': display %12.0fc `r(N)'	
		}
		foreach var of varlist ${others} {
			mean `var'  if ec_dec1 == `ec1' & `var' != -1, over(in_survey one)
			test _subpop_1 = _subpop_2
			local diff_`var'_ec1`ec1': display %3.1f _b[_subpop_2] - _b[_subpop_1]
			local p_`var'_ec1`ec1' = `r(p)'
			local s_`var'_ec1`ec1' = ""
			if `p_`var'_ec1`ec1'' < .1 local s_`var'_ec1`ec1'  = "*"
			if `p_`var'_ec1`ec1'' < .05 local s_`var'_ec1`ec1' = "**"
			if `p_`var'_ec1`ec1'' < .01 local s_`var'_ec1`ec1' = "***"
			lincom _subpop_2 - _subpop_1
			local se_diff_`var'_ec1`ec1': display %3.1f  `r(se)'			
		}
		foreach var of varlist ${zero_to_hundreds} {
			cap drop temp_var
			gen temp_var = `var'/100
			prtest temp_var if ec_dec1 == `ec1' & !inlist(`var', -1, -100), by(in_survey)
			local p_`var'_ec1`ec1' = `r(p)'
			local s_`var'_ec1`ec1' = ""
			if `p_`var'_ec1`ec1'' < .1 local s_`var'_ec1`ec1'  = "*"
			if `p_`var'_ec1`ec1'' < .05 local s_`var'_ec1`ec1' = "**"
			if `p_`var'_ec1`ec1'' < .01 local s_`var'_ec1`ec1' = "***"
			mean `var'  if ec_dec1 == `ec1' & `var' != -1, over(in_survey one)
			local diff_`var'_ec1`ec1': display %3.1f _b[_subpop_2] - _b[_subpop_1]
			lincom _subpop_2 - _subpop_1
			local se_diff_`var'_ec1`ec1': display %3.1f `r(se)'		
		}
	}
	restore

local ec10_panel_title "2nd`=uchar(8211)'10th Decile of Expected Contribution"
local ec11_panel_title "1st Decile of Expected Contribution"
file open tex using "TableA3.tex", write replace

file write tex "\begin{tabular}{l@{\hskip 3em}ccc} " _n
file write tex "\toprule "_n
file write tex " & Respondents & Non-respondents & Difference\\ " _n 
file write tex "\midrule "_n
file write tex "\addlinespace[2mm] "_n
foreach ec1 in 1 0 {
file write tex "\multicolumn{4}{l}{\sc `ec1`ec1'_panel_title'} \\" _n
file write tex "\addlinespace[1mm] "_n
foreach var of varlist  female white Age FIT RNFC SNFC ec {
		file write tex `"``var'_lab'"'
		foreach g in s100 s0  {
			file write tex " &  ``var'_ec1`ec1'_`g''"
		}
		file write tex " & `diff_`var'_ec1`ec1''`s_`var'_ec1`ec1''"
		file write tex " \\" _n
		foreach g in s100 s0  {
			file write tex " &  (`se_`var'_ec1`ec1'_`g'')"
		}
		file write tex " & (`se_diff_`var'_ec1`ec1'')"
		file write tex " \\" _n
	}
    file write tex "\addlinespace[2mm]" _n 
	file write tex  "\$N\$"
	foreach g in s100 s0  {
		file write tex " & `N_ec1`ec1'_`g''"
	}
	file write tex " & "
	file write tex " \\ " _n 
    if (`ec1'==1) {
        file write tex "\addlinespace[6mm]" _n 
    }
    else if (`ec1'==0){
        file write tex "\addlinespace[0.5mm]" _n 
    }
}
file write tex "\bottomrule" _n
file write tex "\end{tabular}"

file close tex	
*/
}

***** TABLE A4: Comparing Survey Incentive Groups
{
	preserve
	gen one = 1
	gen credit_0_temp = 100*(credit_0 == 2) if !missing(credit_0)
	gen credit_1_temp = 100*credit_1
	gen credit_2_temp = 100*credit_2
	gen credit_3_temp = 100*credit_3
	gen credit_4_temp = 100*credit_4
	gen credit_5_temp = 0 if !missing(credit_1_temp)
	replace credit_5_temp = 100 if inlist(1,  credit_5_3, credit_7_3, credit_9_3, ///
												credit_5_2, credit_7_2, credit_9_2)
	gen credit_6_temp = 0 if !missing(credit_1_temp)
	replace credit_6_temp = 100 if inlist(1, 	credit_5_1, credit_7_1, credit_9_1)
	gen AnyDiscourage = 0 if credit_0_temp == 100 & !missing(credit_1)
	replace AnyDiscourage = 100 if inlist(1, credit_4_4, credit_6_4, credit_8_3)

	gen AnyDenDisc = 100*(AnyDiscourage == 100 | credit_6_temp == 100) if !missing(credit_1_temp)
	gen no_credit = 100*(credit_5_temp == 0 & AnyDenDisc==100) if !missing(credit_1_temp)
	
	replace credit_0 = credit_0 == 2 if !missing(credit_0)
	/*	*/
	local compare_vars ///
	FDOS male white senior Age RegionNotFirstChoice SubjectNotFirstChoice ///
					FIT Pell 
	foreach t of numlist 0/2 {
		replace salary_`t' = salary_`t' / 1000
	}
	local Senior_lab "Senior"
	local ismale_lab "Male"
	local female_lab "Female"
	local white_lab "White"
	local Pell_lab "Pell Grant Recipient"
	local ec_lab "Expected Contribution"
	local Age_lab "Age"
	local RegionNotFirstChoice_lab "Region Not First Choice"
	local SubjectNotFirstChoice_lab "Subject Not First Choice"
	local credit_0_temp_lab "Needed additional funds"
	local credit_4_temp_lab "Sought any loan"
	local credit_5_temp_lab "Received any loan"
	local credit_6_temp_lab "Any denial"
	local AnyDiscourage_lab "Any discouragement"
	local AnyDenDisc_lab "Any discouragement or denial"
	local no_credit_lab "No credit access"

	
	local occs_vars teaching_0 teaching_1 forprofit_0 forprofit_1 student_0 student_1
	local tgl_vars credit_0_temp
	local seek_vars credit_4_temp credit_5_temp credit_6_temp AnyDiscourage AnyDenDisc no_credit
	local compare_vars `occs_vars' `tgl_vars' `seek_vars'
				 
	
	foreach d in 0 1 {
		sum in_survey if ec_dec1==`d' & high_incentive == 0 & treatment == 0
		local r_L = `r(mean)'/100
		local se_r_L = `r_L'/(`r(N)')^(1/2)
		sum in_survey if ec_dec1==`d' & high_incentive == 1 & treatment == 0
		local r_H = `r(mean)'/100
		local se_r_H = `r_H'/(`r(N)')^(1/2)
		foreach var of varlist `compare_vars' {
			local binary = 0
			qui sum `var'
			if `r(max)' == 1 & `r(min)' == 0 local binary = 1
			if `binary' == 1 qui replace `var' = 100*`var'
			sum credit_0 if ec_dec1==`d' & high_incentive == 0
			local p_L = `r(mean)'/100
			sum credit_0 if ec_dec1==`d' & high_incentive == 1
			local p_H = `r(mean)'/100
			mean `var' if in_survey == 100 & ec_dec1 == `d', over(high_incentive one)
			local fmt %3.1f 
			local `var'_0_EC1`d': display `fmt' _b[_subpop_1]
			local se_`var'_0_EC1`d': display `fmt' _se[_subpop_1]
			local `var'_1_EC1`d': display `fmt' _b[_subpop_2]
			local se_`var'_1_EC1`d': display `fmt' _se[_subpop_2]
			local diff_`var'_EC1`d': display `fmt' _b[_subpop_2] - _b[_subpop_1]
			lincom _subpop_2 - _subpop_1
			local se_diff_`var'_EC1`d': display `fmt' `r(se)'
			if `binary' == 1 {
				cap drop temp_var 
				gen temp_var = `var'/100
				qui prtest temp_var  if in_survey == 100 & ec_dec1 == `d', by(high_incentive)
			}
			else {
				qui test _subpop_1 = _subpop_2
			}
			local p_`var'_EC1`d' = `r(p)'
			local s_`var'_EC1`d' ""
			if !inlist("`var'", "Agetemp", "salary_0", "salary_1", "salary_2") {
				local unit_`var' "\%"
			}
			if `p_`var'_EC1`d'' < .1 local s_`var'_EC1`d' "*"
			if `p_`var'_EC1`d'' < .05 local s_`var'_EC1`d' "**"
			if `p_`var'_EC1`d'' < .01 local s_`var'_EC1`d' "***"
			local `var'_marg_EC1`d': display `fmt' ``var'_0_EC1`d'' + `r_H'/(`r_H'-`r_L')*(``var'_1_EC1`d'' - ``var'_0_EC1`d'')
			local `var'_margdiff_EC1`d': display `fmt' ``var'_marg_EC1`d'' - ``var'_0_EC1`d''
			local `var'_proj_EC1`d': display `fmt' ``var'_0_EC1`d'' + (1-`r_L')*`r_H'*(``var'_1_EC1`d'' - ``var'_0_EC1`d'')/(`r_H'-`r_L')
			display `"local `var'_proj_EC1`d' = ``var'_0_EC1`d'' + (1-`r_L')*`r_H'*(``var'_1_EC1`d'' - ``var'_0_EC1`d'')/(`r_H'-`r_L')"'
			if strpos("`var'", "`seek_vars'") != 0 		local `var'_proj_EC1`d': display `fmt' ``var'_0_EC1`d'' + (1-`r_L'*`p_L')*`r_H'*`p_H'*(``var'_1_EC1`d'' - ``var'_0_EC1`d'')/(`r_H'*`p_H'-`r_L'*`p_L')
			local se_`var'_marg_EC1`d': display `fmt' 					///
			`= [(`r_L'^2 * `se_`var'_0_EC1`d''^2)/(`r_H'-`r_L')^2		+ 	(`r_H'^2*`se_`var'_1_EC1`d''^2)/(`r_H'-`r_L')^2 		+ (`r_L'^2 * `se_r_H'^2 * (``var'_1_EC1`d'' - ``var'_0_EC1`d'')^2)/(`r_H'-`r_L')^4 	+ (`r_H'^2 * `se_r_L'^2 * (``var'_1_EC1`d'' - ``var'_0_EC1`d'')^2)/(`r_H'-`r_L')^4]^(1/2)'
			local se_`var'_margdiff_EC1`d': display `fmt' 					///
			`= [(`r_H'^2 * `se_`var'_0_EC1`d''^2)/(`r_H'-`r_L')^2		+ 	(`r_H'^2*`se_`var'_1_EC1`d''^2)/(`r_H'-`r_L')^2 		+ (`r_L'^2 * `se_r_H'^2 * (``var'_1_EC1`d'' - ``var'_0_EC1`d'')^2)/(`r_H'-`r_L')^4 	+ (`r_H'^2 * `se_r_L'^2 * (``var'_1_EC1`d'' - ``var'_0_EC1`d'')^2)/(`r_H'-`r_L')^4]^(1/2)'
		}
		qui count if in_survey == 100 & ec_dec1 == `d' & high_incentive == 0 & in_survey == 100
		local N_0_EC1`d'_all = `r(N)'
		qui count if in_survey == 100 & ec_dec1 == `d' & high_incentive == 1 & in_survey == 100
		local N_1_EC1`d'_all = `r(N)'
		qui count if in_survey == 100 & ec_dec1 == `d' & high_incentive == 0 & !missing(credit_4)
		local N_0_EC1`d'_seek = `r(N)'
		qui count if in_survey == 100 & ec_dec1 == `d' & high_incentive == 1 & !missing(credit_4)
		local N_1_EC1`d'_seek = `r(N)'
	}

	local student_0_lab "Graduate student 0 years out"
	local student_1_lab "Graduate student 2 years out"
	file open table using "TableA4.tex", write replace

    file write table "{\sc Mode of Employment Questions} & & & & & & \\" _n
    file write table "\addlinespace[1.5mm]" _n
	foreach var of varlist `occs_vars'  `tgl_vars'{
		file write table `"``var'_lab' (`unit_`var'')"'
		foreach d in 1 0  {
			file write table " & ``var'_0_EC1`d'' & ``var'_1_EC1`d'' & `diff_`var'_EC1`d''`s_`var'_EC1`d''"
		}
		file write table " \\" _n
		foreach d in 1 0  {
			file write table " & (`se_`var'_0_EC1`d'') & (`se_`var'_1_EC1`d'') & (`se_diff_`var'_EC1`d'')"
		}
		file write table " \\" _n
	}
    file write table "\addlinespace[1.5mm]" _n
	file write table "\$N\$ "
	foreach d in 1 0  {
		foreach h in 0 1 {
			file write table "& `N_`h'_EC1`d'_all'"
		}
		file write table " & "
	}
	file write table " \\ " _n
    file write table " \addlinespace[5mm] " _n
    file write table "{\sc Credit Access Questions} & & & & & & \\" _n
    file write table " \addlinespace[1.5mm] " _n
	foreach var of varlist `seek_vars' {
		file write table `"``var'_lab' (`unit_`var'')"'
		foreach d in 1 0  {
			file write table " & ``var'_0_EC1`d'' & ``var'_1_EC1`d'' & `diff_`var'_EC1`d''`s_`var'_EC1`d''"
		}
		file write table " \\" _n
		foreach d in 1 0  {
			file write table " & (`se_`var'_0_EC1`d'') & (`se_`var'_1_EC1`d'') & (`se_diff_`var'_EC1`d'')"
		}
		file write table " \\" _n
	}
    file write table " \addlinespace[1.5mm] " _n
	file write table "\$N\$ "
	foreach d in 1 0  {
		foreach h in 0 1 {
			file write table "& `N_`h'_EC1`d'_seek'"
		}
		file write table " & "
	}
	file write table " \\ " _n
    file write table " \addlinespace[0.5mm] " _n
		
 	file close table
	include clear_locals.do 

	restore
}

***** TABLE A5: Components of Expected Contribution
{
/*
	preserve
	local liquidity_vars = "ec CS PC Income CreditCard POL senior Dependents " 

	local Income_ec_decomp_lab "Income"
	local Dependents_ec_decomp_lab "Number of Dependents"
	local POL_ec_decomp_lab "Private Student Loans"
	local OtherLoans_ec_decomp_lab "Other Student Loans"
	local PC_ec_decomp_lab "Parental Contribution"
	local CS_ec_decomp_lab "Checking and Savings"
	local CreditCard_ec_decomp_lab "Credit Card Debt"
	local senior_ec_decomp_lab "Graduating Senior"

	noisily pcorr `liquidity_vars'

	matrix define A = r(sp_corr)

	reg `liquidity_vars'
	shapley2, stat(r2) 
	matrix define B = e(shapley_rel)
	file open tex using "TableA5.tex", write replace
	file write tex "\begin{tabular}{l|cc} \hline \hline"_n
	file write tex "& \begin{tabular}[c]{@{}c@{}}Squared Semipartial\\ Correlation\end{tabular} & Shapley Value \\ \hline"
	local n = 0
	foreach var in `liquidity_vars' {
		local `var'_spc: display %3.1f 100*A[`n',1]^2
		noisily display `"local `var'_shap: display %3.1f 100*B[`n',1]"'
		local `var'_shap: display %3.1f 100*B[`n',1]
		if "`var'" != "ec" {
			file write tex _n "``var'_ec_decomp_lab' & ``var'_spc'\% & ``var'_shap'\%  \\"
		}
		local n = `n' + 1
	}

	file write tex "\hline" _n "\end{tabular}"
	file close tex
	restore
*/
}

***** TABLE A6: p-values of Balance Tests. NOTE that this does not run because it relies on demographics, which we cannot share.
{
/*
preserve
	local female_lab "Female"
	local white_lab "White"
	local FIT_lab = subinstr("`leftquote'`leftquote'", "\", "", 1) + "Fit" + "\`rightquote'\`rightquote'" + " Score"
	local ec_lab "Expected Contribution"
	local Age_lab "Age"
	local RegionNotFirstChoice_lab "Region Not First Choice"
	local SubjectNotFirstChoice_lab "Subject Not First Choice"

	local demo_vars_for_balance female white Age FIT RegionNotFirstChoice SubjectNotFirstChoice ec 
	local ts 0 1 2 3 4 5
	foreach var of varlist Senior white female Pell {
		replace `var' = `var'*100
	}
	replace ec = ec * 1000

	drop status
	gen status = .
	replace status = 1 if ec_dec == 1
	replace status = 2 if inlist(ec_dec, 2)
	replace status = 3 if inlist(ec_dec, 3,10)

	foreach var in `demo_vars_for_balance' {
		foreach status in 1 2 3 0 {
			foreach t in `ts' {
				if 	(inlist(`t', 0, 1, 2, 3) & `status' == 1)						///
				| 	(inlist(`t', 0, 1, 2, 3) & `status' == 2)						///
				| 	(inlist(`t', 0, 1, 2, 3, 4, 5) & `status' == 3)	{		
					if `status' > 0 qui sum `var' if treatment == `t' & status == `status'
					if `status' == 0 qui sum `var' if treatment == `t'
					local `var'_`status'_`t': display %3.2f `r(mean)'
				}
			}
			if `status' > 0 xi: reg `var' i.treatment if status == `status', absorb(AwardsDate) r
			if `status' == 0 xi: reg `var' i.treatment i.ec_dec, absorb(AwardsDate) r
			if `status' == 0 test 0 = _Itreatment_1 = _Itreatment_2 = _Itreatment_3 = _Itreatment_4 = _Itreatment_5
			if `status' == 1 test 0 = _Itreatment_1 = _Itreatment_2 = _Itreatment_3
			if `status' == 2 test 0 = _Itreatment_1 = _Itreatment_2 = _Itreatment_3
			if `status' == 3 test 0 = _Itreatment_1 = _Itreatment_2 = _Itreatment_3 = _Itreatment_4 = _Itreatment_5
			local p_`var'_`status': display %4.3f `r(p)'
		}
	}

	display `"`FIT_lab'"'


	local title_t0 " Control"

	local treatment_label0 "Control"
	local treatment_label1 "\\\$600 Loan"
	local treatment_label2 "\\\$600 Grant"
	local treatment_label3 "\\\$1,200 Grant"
	local treatment_label4 "\\\$1,800 Grant"
	local treatment_label5 "\\\$1,800 Loan"

	local ts0 0 1 5 2 3 4
	local ts1 0 1 2 3 
	local ts2 0 1 2 3
	local ts3 0 1 5 2 3 4
	file open tex using "TableA6.tex", write replace
	file write tex "\begin{tabular}{l|C{.75in}C{.75in}C{.75in}C{.75in}} \hline \hline"_n
	file write tex "& \multicolumn{4}{c}{Decile of Expected Contribution} \\"
	file write tex "& All & 1st & 2nd & 3rd`=uchar(8211)'10th \\ \hline" 
	foreach var in `demo_vars_for_balance' {
	noisily display `"file write tex _n "``var'_lab'""'
	file write tex _n `"``var'_lab'"'
	foreach status in 0 1 2 3 {
	file write tex " & `p_`var'_`status''"
	}
	file write tex " \\"
	}


	file write tex "\hline" _n "\end{tabular}"
	file close tex
restore
*/
}

***** TABLE A7: Treatment Effects of Additional Grants or Loans, Coefficients from Figures
{
local depvar 	FDOS
local xvars1  	got_GB6 got_GB12 got_LB6				
local xvars2 	got_GB6Xec_dec1 got_GB12Xec_dec1 got_LB6Xec_dec1				///
				got_GB6Xec_dec2 got_GB12Xec_dec2 got_LB6Xec_dec2				///
				got_GB6Xec_dec3 got_GB12Xec_dec3 got_LB6Xec_dec3				///
				got_GB6Xec_dec4 got_GB12Xec_dec4 got_LB6Xec_dec4				///
				got_GB6Xec_dec5 got_GB12Xec_dec5 got_LB6Xec_dec5				///
				got_GB6Xec_dec6 got_GB12Xec_dec6 got_LB6Xec_dec6				///
				got_GB6Xec_dec7 got_GB12Xec_dec7 got_LB6Xec_dec7				///
				got_GB6Xec_dec8 got_GB12Xec_dec8 got_LB6Xec_dec8				///
				got_GB6Xec_dec9 got_GB12Xec_dec9 got_LB6Xec_dec9				///
				got_GB6Xec_dec10 got_GB12Xec_dec10 got_LB6Xec_dec10				///
				ec_dec1 ec_dec2 ec_dec3 ec_dec4 ec_dec5							///			
				ec_dec6 ec_dec7 ec_dec8 ec_dec9 

local xvars3 	got_GB6Xec_dec1 got_GB12Xec_dec1 got_LB6Xec_dec1				///
				got_GB6Xec_dec2 got_GB12Xec_dec2 got_LB6Xec_dec2				///
				got_GB18Xec_dec3 got_LB18Xec_dec3								///
				got_GB18Xec_dec4 got_LB18Xec_dec4								///
				got_GB18Xec_dec5 got_LB18Xec_dec5								///
				got_GB18Xec_dec6 got_LB18Xec_dec6								///
				got_GB18Xec_dec7 got_LB18Xec_dec7								///
				got_GB18Xec_dec8 got_LB18Xec_dec8								///
				got_GB18Xec_dec9 got_LB18Xec_dec9								///
				got_GB18Xec_dec10 got_LB18Xec_dec10								///
				ec_dec1 ec_dec2 ec_dec3 ec_dec4 ec_dec5							///			
				ec_dec6 ec_dec7 ec_dec8 ec_dec9 
				
local xvars4  	got_GB18 got_LB18 				

local xvars5  	got_GB6Xec_dec1 got_GB12Xec_dec1 got_LB6Xec_dec1				///
				got_GB6Xec_dec2_10 got_GB12Xec_dec2_10 got_LB6Xec_dec2_10 		///
				got_GB18Xec_dec2_10 got_LB18Xec_dec2_10							///
				ec_dec1


local depvar FDOS			
local regs 1 2 3 4 5
qui {
foreach r in `regs' {
	noisily display `r'
	if inlist(`r', 1, 2) {
		local cond "year != 2017"
	}
	if inlist(`r', 3) {
		local cond "year == 2017"
	}
	if inlist(`r', 4) {
		local cond "year == 2017 & ec_dec3_10 == 1"
	}
	if inlist(`r', 5) {
		local cond "year != 0"
	}
	local extra_vars ""		
	xi: areg `depvar' `xvars`r'' `extra_vars' if `cond', 	///
									absorb(Batch) r
	matrix define A = r(table)
	local N_`r': display %3.0f `e(N)'
	local r2_`r': display %3.2f `e(r2)'
	qui sum `depvar' if e(sample) == 1
	local ymean_`r': display %3.2f `r(mean)'		
	local i = 0
	foreach var in `xvars`r'' cons {
		local i = `i' + 1
		if "`var'" == "cons" local i = colsof(A)
		local b_`var'_`r': display %3.2f A[1, `i']
		local se_`var'_`r': display %3.2f A[2, `i']
		local p_`var'_`r' = A[4, `i']
		local s_`var'_`r' = ""
		if `p_`var'_`r'' < .1 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
		if `p_`var'_`r'' < .05 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
		if `p_`var'_`r'' < .01 {
			local s_`var'_`r' = "`s_`var'_`r''" + "*"
		}
	}
}

local xvars got_GB6 got_GB12 got_LB6 got_GB18 got_LB18 							///
			got_GB6Xec_dec2_10 got_GB12Xec_dec2_10 got_GB18Xec_dec2_10 got_LB6Xec_dec2_10 got_LB18Xec_dec2_10	///
			got_GB6Xec_dec1 got_GB6Xec_dec2 got_GB6Xec_dec3 got_GB6Xec_dec4 	///
			got_GB6Xec_dec5 got_GB6Xec_dec6 got_GB6Xec_dec7 got_GB6Xec_dec8		///
			got_GB6Xec_dec9 got_GB6Xec_dec10									///
			got_GB12Xec_dec1 got_GB12Xec_dec2 got_GB12Xec_dec3 got_GB12Xec_dec4 	///
			got_GB12Xec_dec5 got_GB12Xec_dec6 got_GB12Xec_dec7 got_GB12Xec_dec8		///
			got_GB12Xec_dec9 got_GB12Xec_dec10									///
			got_LB6Xec_dec1 got_LB6Xec_dec2 got_LB6Xec_dec3 got_LB6Xec_dec4 	///
			got_LB6Xec_dec5 got_LB6Xec_dec6 got_LB6Xec_dec7 got_LB6Xec_dec8		///
			got_LB6Xec_dec9 got_LB6Xec_dec10									///
			got_GB18Xec_dec3 got_GB18Xec_dec4 	///
			got_GB18Xec_dec5 got_GB18Xec_dec6 got_GB18Xec_dec7 got_GB18Xec_dec8		///
			got_GB18Xec_dec9 got_GB18Xec_dec10									///
			got_LB18Xec_dec3 got_LB18Xec_dec4 	///
			got_LB18Xec_dec5 got_LB18Xec_dec6 got_LB18Xec_dec7 got_LB18Xec_dec8		///
			got_LB18Xec_dec9 got_LB18Xec_dec10									///
							
local xvars got_GB6 got_GB12 got_LB6 got_GB18 got_LB18							///
			got_GB6Xec_dec1 got_GB12Xec_dec1 got_LB6Xec_dec1					///
			got_GB6Xec_dec2_10 got_GB12Xec_dec2_10 got_LB6Xec_dec2_10 			///
			got_GB18Xec_dec2_10 got_LB18Xec_dec2_10							///
			got_GB6Xec_dec2 got_GB12Xec_dec2 got_LB6Xec_dec2 				///
			got_GB6Xec_dec3 got_GB12Xec_dec3 got_LB6Xec_dec3 got_GB18Xec_dec3 got_LB18Xec_dec3				///
			got_GB6Xec_dec4 got_GB12Xec_dec4 got_LB6Xec_dec4 got_GB18Xec_dec4 got_LB18Xec_dec4				///
			got_GB6Xec_dec5 got_GB12Xec_dec5 got_LB6Xec_dec5 got_GB18Xec_dec5 got_LB18Xec_dec5				///				
			got_GB6Xec_dec6 got_GB12Xec_dec6 got_LB6Xec_dec6 got_GB18Xec_dec6 got_LB18Xec_dec6					///
			got_GB6Xec_dec7 got_GB12Xec_dec7 got_LB6Xec_dec7 got_GB18Xec_dec7 got_LB18Xec_dec7					///
			got_GB6Xec_dec8 got_GB12Xec_dec8 got_LB6Xec_dec8 got_GB18Xec_dec8 got_LB18Xec_dec8				///
			got_GB6Xec_dec9 got_GB12Xec_dec9 got_LB6Xec_dec9 got_GB18Xec_dec9 got_LB18Xec_dec9				///
			got_GB6Xec_dec10 got_GB12Xec_dec10 got_LB6Xec_dec10	got_GB18Xec_dec10 got_LB18Xec_dec10


file open tex using "TableA7.tex", write replace
foreach var in `xvars'  {
	file write tex  "``var'_b_lab'"
	foreach r in `regs' {
		file write tex "& `b_`var'_`r''`s_`var'_`r''"
	}
	file write tex " \\" _n
	file write tex  "``var'_se_lab'"
	foreach r in `regs' {
		if "`se_`var'_`r''" != "" {
			file write tex `"& (`se_`var'_`r'')"'
		}
		else {
			file write tex `"& "'
		}
	}
	file write tex " \\" _n
}

file write tex "Demographics & No & No & No & No & No"
file write tex " \\" _n "Batch FEs "
foreach r in `regs' {
		file write tex " & Yes "
}
file write tex " \\" _n "\$N\$"
foreach r in `regs' {
	file write tex " & `N_`r''"
}

file write tex " \\" _n "\$R^2\$"
foreach r in `regs' {
		file write tex " & `r2_`r''"
}
file write tex " \\" _n "Mean of Dep. Var."
foreach r in `regs' {
	file write tex " & `ymean_`r''"
}
file write tex " \\ \hline"
file close tex
include clear_locals.do 

}
}

***** TABLE A8: Treatment Effects of Additional Grants or Loans, All Coefficients
{
	local depvar FDOS
	local regs 1 3 5
	local xvars1 "GB_amount LB_amount"				
	local xvars 	GB_amountXec_dec1 LB_amountXec_dec1 						///
					GB_amountXec_dec2 LB_amountXec_dec2 					///
					GB_amountXec_dec3 LB_amountXec_dec3 					///
					GB_amountXec_dec4 LB_amountXec_dec4 					///
					GB_amountXec_dec5 LB_amountXec_dec5 					///
					GB_amountXec_dec6 LB_amountXec_dec6 					///
					GB_amountXec_dec7 LB_amountXec_dec7 					///
					GB_amountXec_dec8 LB_amountXec_dec8 					///
					GB_amountXec_dec9 LB_amountXec_dec9 					///
					GB_amountXec_dec10 LB_amountXec_dec10 					///
					ec_dec1 ec_dec2 ec_dec3 ec_dec4 ec_dec5					///			
					ec_dec6 ec_dec7 ec_dec8 ec_dec9 
	local depvar FDOS			
	qui {
	foreach r in `regs' {
		noisily display `r'
		if inlist(`r', 1, 2) {
			local cond "year != 2017"
		}
		if inlist(`r', 3, 4) {
			local cond "year == 2017"
		}
		if inlist(`r', 5, 6) {
			local cond "year != 0"
		}
		if inlist(`r', 1, 3, 5) {
			local extra_vars ""		
		}
		if inlist(`r', 2, 4, 6) {
			local extra_vars "${demos}"		
		}
		xi: areg `depvar' `xvars' `extra_vars' if `cond', 	///
										absorb(Batch) r
		matrix define A = r(table)
		local N_`r': display %3.0f `e(N)'
		local r2_`r': display %3.2f `e(r2)'
		qui sum `depvar' if e(sample) == 1
		local ymean_`r': display %3.2f `r(mean)'		
		local i = 0
		foreach var in `xvars' cons {
			local i = `i' + 1
			if "`var'" == "cons" local i = colsof(A)
			local b_`var'_`r': display %3.2f A[1, `i']
			local se_`var'_`r': display %3.2f A[2, `i']
			local p_`var'_`r' = A[4, `i']
			local s_`var'_`r' = ""
			if `p_`var'_`r'' < .1 {
				local s_`var'_`r' = "`s_`var'_`r''" + "*"
			}
			if `p_`var'_`r'' < .05 {
				local s_`var'_`r' = "`s_`var'_`r''" + "*"
			}
			if `p_`var'_`r'' < .01 {
				local s_`var'_`r' = "`s_`var'_`r''" + "*"
			}
		}
	}

	local regs 1 2 3 4 5 6
	file open tex using "TableA8.tex", write replace
	file write tex "\begin{tabular}{l|cc|cc|cc} \hline \hline"_n
	file write tex _n "& \multicolumn{2}{c|}{2015`=uchar(8211)'2016} & \multicolumn{2}{c|}{2017} & \multicolumn{2}{c}{2015`=uchar(8211)'2017} \\"
	file write tex  _n "& (1) & (2) & (3) & (4) & (5) & (6) \\ \hline" _n
	foreach var in `xvars'  {
		file write tex  "``var'_b_lab'"
		foreach r in `regs' {
			file write tex "& `b_`var'_`r''`s_`var'_`r''"
		}
		file write tex " \\" _n
		file write tex  "``var'_se_lab'"
		foreach r in `regs' {
			if "`se_`var'_`r''" != "" {
				file write tex `"& (`se_`var'_`r'')"'
			}
			else {
				file write tex `"& "'
			}
		}
		file write tex " \\" _n
	}
	file write tex "Demographics & No & Yes & No & Yes & No & Yes"
	file write tex " \\" _n "Batch FEs "
	foreach r in `regs' {
			file write tex " & Yes "
	}
	file write tex " \\" _n "\$N\$"
	foreach r in `regs' {
		file write tex " & `N_`r''"
	}

	file write tex " \\" _n "\$R^2\$"
	foreach r in `regs' {
			file write tex " & `r2_`r''"
	}
	file write tex " \\" _n "Mean of Dep. Var."
	foreach r in `regs' {
		file write tex " & `ymean_`r''"
	}
	file write tex " \\ \hline" _n "\end{tabular}"
	file close tex
	include clear_locals.do 

	}
}

***** TABLE A9: Reasons Respondents Did Not Seek Various Sources of Credit
{
	local reason_0_lab "Too time consuming"
	local reason_1_lab "Borrowing rates too high"
	local reason_2_lab "Did not know how"
	local reason_3_lab "Did not occur to me"
	local reason_4_lab "Thought request would be denied"
	local reason_5_lab "Covered need another way"
	local reason_6_lab "Was not willing"
	local reason_7_lab "Too much strain on relationships"
	local reason_8_lab "No one had enough money to ask"
	local reason_9_lab "Other"

	gen credit_reason_0 = credit_4_0
	gen credit_reason_1 = credit_4_1
	gen credit_reason_2 = credit_4_2
	gen credit_reason_3 = credit_4_3
	gen credit_reason_4 = credit_4_4
	gen credit_reason_5 = credit_4_5
	gen credit_reason_6 = credit_4_6
	gen credit_reason_9 = credit_4_7

	gen loan_reason_0 = credit_6_0
	gen loan_reason_1 = credit_6_1
	gen loan_reason_2 = credit_6_2
	gen loan_reason_3 = credit_6_3
	gen loan_reason_4 = credit_6_4
	gen loan_reason_5 = credit_6_5
	gen loan_reason_6 = credit_6_6
	gen loan_reason_9 = credit_6_7

	gen informal_reason_3 = credit_8_2
	gen informal_reason_4 = credit_8_3
	gen informal_reason_5 = credit_8_4
	gen informal_reason_6 = credit_8_5
	gen informal_reason_9 = credit_8_6
	gen informal_reason_7 = credit_8_0
	gen informal_reason_8 = credit_8_1
	
	foreach type in credit loan informal {
		foreach n of numlist 0/9 {
			capture confirm variable `type'_reason_`n' 
			if !_rc {
				foreach d of numlist 0/1 {
					sum `type'_reason_`n' if ec_dec1 == `d'
					local `type'_`n'_EC1_`d': display %3.1f 100*`r(mean)'
					local N_`type'_`n'_EC1_`d' = `r(N)'
				}
				qui prtest `type'_reason_`n', by(ec_dec1)
				*qui test _subpop_1 = _subpop_2 
				local p_`type'_`n'_EC1_1 = `r(p)'
				local s_`type'_`n'_EC1_1 ""
				if `p_`type'_`n'_EC1_1' < .1 local s_`type'_`n'_EC1_1 = "*"
				if `p_`type'_`n'_EC1_1' < .05 local s_`type'_`n'_EC1_1 = "**"
				if `p_`type'_`n'_EC1_1' < .01 local s_`type'_`n'_EC1_1 = "***"
			}
		}
	}
	
	
	
	file open table using "TableA9.tex", write replace
	foreach n of numlist 0/9 {
		file write table "`reason_`n'_lab'"		
		foreach type in credit loan informal {
			foreach d of numlist 1/0 {
				if "``type'_`n'_EC1_`d''" != "" {
					file write table "& ``type'_`n'_EC1_`d''\%"	
				}
				else {
					file write table "&"					
				}
			}
		}
		file write table " \\" _n
	}
	file write table "\$N\$"
	foreach type in credit loan informal {
		foreach d of numlist 1/0 {
			file write table " & `N_`type'_3_EC1_`d''"
		}
	}
	file write table " \\ \hline"
	file close table
	
}

***** TABLE A10: Credit Request Outcomes
{
	local outcome_0_lab "Prefer not to answer"
	local outcome_1_lab "Rejected"
	local outcome_2_lab "Partially granted"
	local outcome_3_lab "Fully granted"

	foreach c of numlist 5 7 9 {
		foreach n of numlist 0/3 {
			foreach d of numlist 0/1 {
				qui sum credit_`c'_`n' if ec_dec1 == `d'
				local outcome_`c'_`n'_EC1_`d': display %3.1f 100*`r(mean)'
				local N_`c'_`n'_EC1_`d' = `r(N)'
			}
			qui prtest credit_`c'_`n', by(ec_dec1)
			*qui test _subpop_1 = _subpop_2 
			local p_`c'_`n'_EC1_1 = `r(p)'
			local s_`c'_`n'_EC1_1 ""
			if `p_`c'_`n'_EC1_1' < .1 local s_`c'_`n'_EC1_1 = "*"
			if `p_`c'_`n'_EC1_1' < .05 local s_`c'_`n'_EC1_1 = "**"
			if `p_`c'_`n'_EC1_1' < .01 local s_`c'_`n'_EC1_1 = "***"
		}
	}
	file open table using "TableA10.tex", write replace
	foreach n of numlist 0/3 {
		file write table "`outcome_`n'_lab'"
		foreach c of numlist 5 7 9 {
			foreach d of numlist 1/0 {
				file write table "& `outcome_`c'_`n'_EC1_`d''\%"
			}
		}
		file write table " \\" _n
	}
	file write table "\$N\$"
	foreach c of numlist 5 7 9 {
		foreach d of numlist 1/0 {
			file write table "& `N_`c'_1_EC1_`d''"
		}
	}
	file write table " \\ \hline"
	file close table
}

***** TABLE A11: Treatment Effects on Actual and Expected Occupations, All Coefficients. NOTE that this does not run because it relies on demographics, which we cannot share.
{
/*
	preserve
	local depvar_1 FDOS
	local depvar_2 teaching_0
	local depvar_3 teaching_1
	local depvar_4 teaching_2
	local depvar_5 forprofit_0
	local depvar_6 forprofit_1
	local depvar_7 forprofit_2
	local depvar_8 student_0
	local depvar_9 student_1

	local cond "!missing(employment_0)"
	local xvars AB_amountXec_dec1 AB_amountXec_dec2 AB_amountXec_dec3 AB_amountXec_dec4 AB_amountXec_dec5 AB_amountXec_dec6 AB_amountXec_dec7 AB_amountXec_dec8 AB_amountXec_dec9 AB_amountXec_dec10 ec_dec1 ec_dec2 ec_dec3 ec_dec4 ec_dec5 ec_dec6 ec_dec7 ec_dec8 ec_dec9
	local regs 1 2 3 4 5 6 7 8 9 

	foreach r in `regs' {
		local num_pos_sig_`r' = 0
		local num_neg_sig_`r' = 0	
		local extra_vars "${demos} "
		qui xi: areg `depvar_`r'' `xvars'  `extra_vars'   if `cond', 	///
										absorb(AwardsDate) r
		matrix define A = r(table)
		local N_`r': display %3.0f `e(N)'
		local r2_`r': display %3.2f `e(r2)'
		qui sum `depvar_`r'' if e(sample) == 1
		local ymean_`r': display %3.2f `r(mean)'		
		local i = 0
		foreach var in `xvars'   cons {
			local i = `i' + 1
			if "`var'" == "cons" local i = colsof(A)
			local b_`var'_`r': display %3.2f A[1, `i']
			local se_`var'_`r': display %3.2f A[2, `i']
			local p_`var'_`r' = A[4, `i']
			local s_`var'_`r' = ""
			if 	`p_`var'_`r'' < .1 & 								///
				strpos("`var'", "AB_amountX") != 0 & 				///
				!inlist("`var'", "AB_amountXec_dec1") ///
			{
				if `b_`var'_`r'' > 0 {
					local num_pos_sig_`r' = `num_pos_sig_`r'' + 1
				}
				if `b_`var'_`r'' < 0 {
					local num_neg_sig_`r' = `num_neg_sig_`r'' + 1
				}
			}
			if `p_`var'_`r'' < .1 {
				local s_`var'_`r' = "`s_`var'_`r''" + "*"
			}
			if `p_`var'_`r'' < .05 {
				local s_`var'_`r' = "`s_`var'_`r''" + "*"
			}
			if `p_`var'_`r'' < .01 {
				local s_`var'_`r' = "`s_`var'_`r''" + "*"
			}
		}	
	}

	file open tex using "TableA11.tex", write replace
	foreach var in `xvars'   {
		if !inlist("`var'", "AB_amount") {
			file write tex  "``var'_b_lab'"
			foreach r in `regs' {
				file write tex "& `b_`var'_`r''`s_`var'_`r''"
			}
			file write tex " \\" _n
			file write tex  "``var'_se_lab'"
			foreach r in `regs' {
				if "`se_`var'_`r''" != "" {
					file write tex `"& (`se_`var'_`r'')"'
				}
				else {
					file write tex `"& "'
				}
			}
			file write tex " \\" _n
		}
	}

	file write tex /*" \\" _n*/ "\$N\$"
	foreach r in `regs' {
		file write tex " & `N_`r''"
	}

	file write tex " \\" _n "\$R^2\$"
	foreach r in `regs' {
			file write tex " & `r2_`r''"
	}
	file write tex " \\" _n "Mean of Dependent Variable"
	foreach r in `regs' {
		file write tex " & `ymean_`r''"
	}
	file write tex " \\ \hline"
	file close tex

	restore
*/
}

***** FIGURE 1: Control Awards (2015–2017)
{

hist ControlGrant, xtitle("Grants")				///
		graphregion(color(white)) percent width(500) lcolor(`G6_border') lwidth(vthin) fcolor(`G6_color'*.8)	///
		saving(grants, replace) scale(1.5) ytitle("") legend(off) title("Panel (B)", color(black) size(*.75))	///
		gap(10)

hist ControlLoan, xtitle("Loans")				///
		graphregion(color(white)) percent width(250) lcolor(`L6_border') lwidth(vthin) fcolor(`L6_color')	///
		start(250) xlabels(0(1000)3000) ///
		saving(loans, replace) scale(1.5) legend(off)  title("Panel (A)", color(black) size(*.75))	///
		gap(10)

		
hist ControlAward, xtitle("Total Award")				///
		graphregion(color(white)) percent width(500) lcolor(`C_border') lwidth(medium) fcolor(`C_color')	///
		saving(awards, replace) scale(1.5) ytitle("") legend(off)  title("Panel (C)", color(black) size(*.75))	///
		gap(10)
	
graph combine "loans.gph" "grants.gph"  "awards.gph"		///
		, xsize(18) ysize(6) rows(1) ycommon	///
		scale(1.25) graphregion(color(white))

graph export "Figure1.pdf", as(pdf) replace

local files_to_remove grants loans awards
foreach file in `files_to_remove' {
rm "`file'.gph"
}

}

***** FIGURE 2: Control Awards, by Decile of Expected Contribution (2015–2017)
{

preserve

foreach var of varlist ControlGrant ControlLoan ControlAward {
	replace `var' = `var' / 1000
}
collapse (mean) ControlGrant ControlLoan ControlAward
gen ec_dec = 0
tempfile all
save `all'
restore
preserve
foreach var of varlist ControlGrant ControlLoan ControlAward {
	replace `var' = `var' / 1000
}
collapse (mean) ControlGrant ControlLoan ControlAward, by(ec_dec)
append using `all'
reshape long Control, i(ec_dec) j(Type) str

gen Type_n = 3 if Type == "Award"
replace Type_n = 1 if Type == "Loan"
replace Type_n = 2 if Type == "Grant"


		local d1_title "1st"
		local d2_title "2nd"
		local d3_title "3rd"
		local d4_title "4th"
		local d5_title "5th"
		local d6_title "6th"
		local d7_title "7th"
		local d8_title "8th"
		local d9_title "9th"
		local d10_title "10th"

graph twoway 	(bar Control Type_n if Type_n == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border')  lwidth(vthin))									///
				(bar Control Type_n if Type_n == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border')  lwidth(vthin))	///
				(bar Control Type_n if Type_n == 3, barwidth(.9) fcolor(`C_color') lcolor(`C_border')  lwidth(vthin))	///
		if inlist(ec_dec,0), ylabel(0(1)5) yscale(range(0 5) )							///
		title(All, size(medsmall) color(black)) 	ytitle("Mean Award ($1000s)")				///
		graphregion(color(white)) 					///
		scale(1.5) fysize(100) fxsize(125)					///
		xlabel(1(1)2,nolabel tlcolor(none)) xscale(range(1 2)) xtitle("") 	/// ///
		legend(off order(1 "Loans" 2 "Grants" 3 "Total Award") rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.6)  bmargin(zero) size(*.7)) ///
		saving(Awards_DecileAll, replace)   


		
graph twoway 	(bar Control Type_n if Type_n == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border')  lwidth(vthin))									///
				(bar Control Type_n if Type_n == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border')  lwidth(vthin))	///
				(bar Control Type_n if Type_n == 3, barwidth(.9) fcolor(`C_color') lcolor(`C_border')  lwidth(vthin))	///
		if inlist(ec_dec,1), ylabel(0(1)5,  nolab tlength(0) tlcolor(white)) yscale(range(0 5) lpattern(longdash))							///
		title(`d1_title', size(medsmall) color(black)) 	ytitle("")				///
		graphregion(color(white)) 					///
		scale(1.5) fysize(100) fxsize(70)					///
		xlabel(1(1)2,nolabel tlcolor(none)) xscale(range(1 2)) xtitle("") 	/// ///
		legend(off) ///
		saving(Awards_Decile1, replace)   

foreach d of numlist 2/10 {
graph twoway 	(bar Control Type_n if Type_n == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border')  lwidth(vthin))									///
				(bar Control Type_n if Type_n == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border')  lwidth(vthin))	///
				(bar Control Type_n if Type_n == 3, barwidth(.9) fcolor(`C_color') lcolor(`C_border')  lwidth(vthin))	///
		if inlist(ec_dec,`d'), ylabel(0(1)5,  nolab tlength(0) tlcolor(white)) yscale(range(0 5) noline)							///
		title(`d`d'_title', size(medsmall) color(black)) 	ytitle("")				///
		graphregion(color(white)) 					///
		scale(1.5) fysize(100) fxsize(70)					///
		xlabel(1(1)2,nolabel tlcolor(none)) xscale(range(1 2)) xtitle("") 	/// ///
		legend(off) ///
		saving(Awards_Decile`d', replace)  
		 
}

grc1leg   							///
				"Awards_DecileAll.gph"							///
				"Awards_Decile1.gph"							///
				"Awards_Decile2.gph"							///
				"Awards_Decile3.gph"							///
				"Awards_Decile4.gph"							///
				"Awards_Decile5.gph"							///
				"Awards_Decile6.gph"							///
				"Awards_Decile7.gph"							///
				"Awards_Decile8.gph"							///
				"Awards_Decile9.gph"							///
				"Awards_Decile10.gph"							///
				, rows(1)	ycommon	///
		scale(1.3) graphregion(color(white)) imargin(0 0 0 0)				///
		title(Decile of Expected Contribution, size(*.6) color(black))					///
		saving(AwardsByDecile, replace)

		graph combine "AwardsByDecile.gph", 					///
	graphregion(color(white)) xsize(20) ysize(8) scale(1.7)		///
	saving(Awards_ByEC_Decile, replace)
graph export "Figure2.pdf", as(pdf) replace

local files_to_remove 	Awards_DecileAll Awards_Decile1 Awards_Decile2 			///
						Awards_Decile3 Awards_Decile4 Awards_Decile5			///
						Awards_Decile6 Awards_Decile7 Awards_Decile8			///
						Awards_Decile9 Awards_Decile10 Awards_ByEC_Decile		///
						AwardsByDecile
						
foreach file in `files_to_remove' {
rm "`file'.gph"
}

restore

}

***** FIGURE 3: Treatment Effects of Additional Grants and Loans (2015–2016)
{
	preserve
	keep if cohort != 2017

	gen decile_for_figure = 0 if _n == 1
	foreach n of numlist 1/10 {
		gen G6Xec_dec_`n' = got_GB6*ec_dec_`n'
		gen G12Xec_dec_`n' = got_GB12*ec_dec_`n'
		gen L6Xec_dec_`n' = got_LB6*ec_dec_`n'
	}
	foreach n of numlist 1/12 {
		replace decile_for_figure = `n' if _n == 1+`n'
	}
	drop ec_dec_10
	areg FDOS got_GB6 got_GB12 got_LB6, absorb(Batch)
	gen G6 = _b[got_GB6] if decile_for_figure == 0
	gen G12 = _b[got_GB12] if decile_for_figure == 0
	gen L6 = _b[got_LB6] if decile_for_figure == 0
	gen se_G6 = _se[got_GB6] if decile_for_figure == 0
	gen se_G12 = _se[got_GB12] if decile_for_figure == 0
	gen se_L6 = _se[got_LB6] if decile_for_figure == 0

	noisily areg FDOS G6Xec_dec_* G12Xec_dec_* L6Xec_dec_* ec_dec_*, absorb(Batch) r
	foreach d of numlist 1/9 {
		replace G6 = _b[G6Xec_dec_`d'] if decile_for_figure == `d'
		replace G12 =  _b[G12Xec_dec_`d'] if decile_for_figure == `d'
		replace L6 = _b[L6Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G6 = _se[G6Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G12 = _se[G12Xec_dec_`d'] if decile_for_figure == `d'
		replace se_L6 = _se[L6Xec_dec_`d'] if decile_for_figure == `d'
	}
	local d = 10
	replace G6 = _b[G6Xec_dec_`d'] if decile_for_figure == `d' 
	replace G12 =  _b[G12Xec_dec_`d'] if decile_for_figure == `d' 
	replace L6 = _b[L6Xec_dec_`d'] if decile_for_figure == `d'
	replace se_G6 = _se[G6Xec_dec_`d'] if decile_for_figure == `d'
	replace se_G12 = _se[G12Xec_dec_`d'] if decile_for_figure == `d'
	replace se_L6 = _se[L6Xec_dec_`d'] if decile_for_figure == `d'

	gen G6Xec_dec_2_10 = got_GB6*(ec_dec_1 == 0)
	gen G12Xec_dec_2_10 = got_GB12*(ec_dec_1 == 0)
	gen L6Xec_dec_2_10 = got_LB6*(ec_dec_1 == 0)
	
	collapse (mean) G6 G12 L6 se_G6 se_G12 se_L6, by(decile_for_figure)
	rename (G6 se_G6 L6 se_L6 G12 se_G12) (b_1 se_1 b_2 se_2 b_3 se_3)
	reshape long b_ se_, i(decile_for_figure) j(treatment)
	rename (*_) (*)
	recode treatment (0=0) (1=2) (2=1) (3=3) (4=5) (5=4)
	label define t_lab 0 "Control" 1 "$600 Loan" 2 "$600 Grant" 3 "$1200 Grant", 
	label values treatment t_lab

	gen L = b - se
	gen U = b + se 

	local d1_title "1st"
	local d2_title "2nd"
	local d3_title "3rd"
	local d4_title "4th"
	local d5_title "5th"
	local d6_title "6th"
	local d7_title "7th"
	local d8_title "8th"
	local d9_title "9th"
	local d10_title "10th"


	local ymin_temp -10
	local ymax_temp 20
	graph twoway 	(bar b treatment if treatment == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))	///
					(bar b treatment if treatment == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))	///
					(rcap U L treatment if treatment < 3, color(`cap_color') msize(medium) lwidth(medium)) if inlist(decile_for_figure, 0),			///
			ylabel(`ymin_temp'(10)`ymax_temp',) yscale(range(`ymin_temp' `ymax_temp') )							///
			title(All, size(medsmall) color(black)) 	ytitle("Treatment Effect (pp)")						///
			graphregion(color(white)) 					///
			scale(1.5) fysize(100) fxsize(130)					///
			 xtitle("")				 xlabel(1(1)2,nolabel tlcolor(none))				///
			legend(off order(1 "$600 Loan" 2 "$600 Grant" ) rows(1) ///
			 region(lcolor(white)) symysize(*.6) symxsize(*.6)  bmargin(zero) size(*.7)) 	///
			 saving(Figure3_all, replace)	 

	graph twoway 	(bar b treatment if treatment == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))	///
					(bar b treatment if treatment == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))	///
					(rcap U L treatment if treatment < 3, color(`cap_color') msize(medium) lwidth(medium)) if inlist(decile_for_figure, 1),			///
			ylabel(`ymin_temp'(10)`ymax_temp', nolab tlength(0) tlcolor(white)) yscale(range(`ymin_temp' `ymax_temp') lpattern(longdash))							///
			title(`d1_title', size(medsmall) color(black))							///
			graphregion(color(white))							///
			scale(1.5) fysize(100) fxsize(70) 					///
			xlabel(1(1)2,nolabel tlcolor(none))  xtitle("") 	///
			 saving(Figure3_D1, replace) legend(off) 		

	foreach d of numlist 2/10 {
	graph twoway 	(bar b treatment if treatment == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))	///
					(bar b treatment if treatment == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))	///
					(rcap U L treatment if treatment < 3, color(`cap_color') msize(medium) lwidth(medium)) if inlist(decile_for_figure, `d'),			///
			ylabel(`ymin_temp'(10)`ymax_temp', nolab tlength(0) tlcolor(white)) yscale(range(`ymin_temp' `ymax_temp') noline)							///
			title(`d`d'_title', size(medsmall) color(black))							///
			graphregion(color(white))							///
			scale(1.5) fysize(100) fxsize(70) 					///
			xlabel(1(1)2,nolabel tlcolor(none) labsize(*.6))  xtitle("") 	///
			 saving(Figure3_D`d', replace) legend(off) 		
	}		 

	grc1leg    							///
					"Figure3_all.gph"							///
					"Figure3_D1.gph"							///
					"Figure3_D2.gph"							///
					"Figure3_D3.gph"							///
					"Figure3_D4.gph"							///
					"Figure3_D5.gph"							///
					"Figure3_D6.gph"							///
					"Figure3_D7.gph"							///
					"Figure3_D8.gph"							///
					"Figure3_D9.gph"							///
					"Figure3_D10.gph"							///
					,rows(1)	ycommon	///
			scale(1.3) graphregion(color(white)) imargin(0 0 0 0)				///
			title(Decile of Expected Contribution, size(*.6) color(black))					///
			saving(Figure3, replace)
			
	graph combine "Figure3",						///
			graphregion(color(white)) xsize(20) ysize(10) scale(1.3)
	graph export "Figure3.pdf", as(pdf) replace
	
	gen year = 2016
	tempfile datapre2017
	save `datapre2017'
	local files_to_remove 	Figure3_all Figure3_D1 Figure3_D2 Figure3_D3 			///
							Figure3_D4 Figure3_D5 Figure3_D6			///
							Figure3_D7 Figure3_D8 Figure3_D9			///
							Figure3_D10 Figure3
							
	foreach file in `files_to_remove' {
	rm "`file'.gph"
	}
	restore
}

***** FIGURE 4: Replication of Treatment Effects
***** and FIGURE A4: Stress Test of Null Results (2017)
{
	preserve
	local ymin -20
	local ymax 40
	keep if cohort == 2017

	gen decile_for_figure = .
	foreach n of numlist 1/2 {
		gen G6Xec_dec_`n' = got_GB6*ec_dec_`n'
		gen L6Xec_dec_`n' = got_LB6*ec_dec_`n'
		gen G12Xec_dec_`n' = got_GB12*ec_dec_`n'
		replace decile_for_figure = `n' if _n == 1+`n'
	}
	foreach n of numlist 3/10 {
		gen G18Xec_dec_`n' = got_GB18*ec_dec_`n'
		gen L18Xec_dec_`n' = got_LB18*ec_dec_`n'
		replace decile_for_figure = `n' if _n == 1+`n'
	}
	replace decile_for_figure = 11 if _n==12
	drop ec_dec_10
	gen G = .
	gen L = .
	gen G12 = .
	gen se_G = .
	gen se_L = .
	gen se_G12 = .


	
	areg FDOS G6Xec_dec_* L6Xec_dec_* G12Xec_dec_* G18Xec_dec_* L18Xec_dec_* ec_dec_*, absorb(Batch) r
	foreach d of numlist 1/2 {
		replace G = _b[G6Xec_dec_`d'] if decile_for_figure == `d'
		replace L = _b[L6Xec_dec_`d'] if decile_for_figure == `d'
		replace G12 =  _b[G12Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G = _se[G6Xec_dec_`d'] if decile_for_figure == `d'
		replace se_L = _se[L6Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G12 =  _se[G12Xec_dec_`d'] if decile_for_figure == `d'
	}
	foreach d of numlist 3/9 {
		replace G = _b[G18Xec_dec_`d'] if decile_for_figure == `d'
		replace L = _b[L18Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G = _se[G18Xec_dec_`d'] if decile_for_figure == `d'
		replace se_L = _se[L18Xec_dec_`d'] if decile_for_figure == `d'
	}
	local d = 10
	replace G = _b[G18Xec_dec_`d'] if decile_for_figure == `d'
	replace L = _b[L18Xec_dec_`d'] if decile_for_figure == `d'
	replace se_G = _se[G18Xec_dec_`d'] if decile_for_figure == `d'
	replace se_L = _se[L18Xec_dec_`d'] if decile_for_figure == `d'

		noisily areg FDOS got_GB18 got_LB18 if inrange(ec_dec, 3,10), absorb(Batch) r
		local d = 11
		replace G = _b[got_GB18] if decile_for_figure == `d'
		replace L = _b[got_LB18] if decile_for_figure == `d'
		replace se_G = _se[got_GB18] if decile_for_figure == `d'
		replace se_L = _se[got_LB18] if decile_for_figure == `d'
		

	collapse (mean) G L se_G se_L G12 se_G12, by(decile_for_figure)
	rename (G se_G L se_L G12 se_G12) (b_1 se_1 b_2 se_2 b_3 se_3)
	reshape long b_ se_, i(decile_for_figure) j(treatment)
	rename (*_) (*)
	recode treatment (0=0) (1=2) (2=1) (3=3) (4=5) (5=4)
	drop if treatment == 3 & inrange(decile_for_figure, 3, 11)
	replace treatment = treatment + 10 if inrange(decile_for_figure, 3, 11)
	
	label define t_lab 0 "Control" 1 "$600 Loan" 2 "$600 Grant" 3 "$1200 Grant"	///
						10 "Control" 11 "$1800 Loan" 12 "$1800 Grant", 
	label values treatment t_lab

	gen L = b - se
	gen U = b + se 


		local d1_title "1st"
		local d2_title "2nd"
		local d3_title "3rd"
		local d4_title "4th"
		local d5_title "5th"
		local d6_title "6th"
		local d7_title "7th"
		local d8_title "8th"
		local d9_title "9th"
		local d10_title "10th"

	label define t_labs 0 "Control" 1 "$600 Loan" 2 "$600 Grant" 3 "$1200 Grant" 10 "Control" 11 "$600 Loan" 12 "$600 Grant"
	
		tempfile temp
		save `temp'
		
		replace b = 0 if inrange(decile_for_figure, 1,2)
		replace U = 0 if inrange(decile_for_figure, 1,2)
		replace L = 0 if inrange(decile_for_figure, 1,2)
				graph twoway 	(bar b treatment if treatment == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))	///
					(bar b treatment if treatment == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))	///
					if inlist(decile_for_figure, 1),			///
			ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin_temp' `ymax_temp') lpattern(longdash))							///
			title(`d1_title', size(*1) color(black))							///
			graphregion(color(white))			ytitle("")						///
			scale(1.5) fysize(100) fxsize(70) 					///
			xlabel(1(1)2,nolabel tlcolor(none))  xtitle("") 	///
			 saving(FigureA4_D1, replace) legend(off) 		
		
		graph twoway 	(bar b treatment if treatment == 1, barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))	///
					(bar b treatment if treatment == 2, barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))	///
					if inlist(decile_for_figure, 2),			///
			ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin_temp' `ymax_temp') noline)							///
			title(`d2_title', size(*1) color(black))							///
			graphregion(color(white))	ytitle("")						///
			scale(1.5) fysize(100) fxsize(70) 					///
			xlabel(1(1)2,nolabel tlcolor(none))  xtitle("") 	///
			 saving(FigureA4_D2, replace) legend(off) 		
		
		use `temp', clear

	
		graph twoway 	(bar b treatment if treatment == 11 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
						(bar b treatment if treatment == 12 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
						(rcap U L treatment, color(`cap_color') msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 11),			///
				ylabel(-20(10)30, labsize(*1.3) ) yscale(range(-20 30))							///
				title("3rd`=uchar(8211)'10th", size(*1) color(black))							///
				graphregion(color(white))	ytitle("Treatment Effect (pp)", size(*1.3))						///
				scale(1.5) fysize(100) fxsize(150)	xscale(range(10 13))				///
				xlabel(11(1)12,nolabel tlcolor(none) labsize(*.6) angle(45))  xtitle("") 	///
				 saving(FigureA4_D3to10, replace) 			///
				 legend(off order(1 "$1800 Loan" 2 "$1800 Grant") ///
				 rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.5)  bmargin(zero) size(*1)) 


		foreach d of numlist 3/10 {
		graph twoway 	(bar b treatment if treatment == 11 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
						(bar b treatment if treatment == 12 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
						(rcap U L treatment, color(`cap_color') msize(medium) lwidth(medthin)) if inlist(decile_for_figure, `d'),			///
				ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin' `ymax') noline)							///
				title(`d`d'_title', size(*1) color(black))							///
				graphregion(color(white))							///
				scale(1.5) fysize(100) fxsize(70) 					///
				xlabel(11(1)12,nolabel tlcolor(none) labsize(*.6) angle(45))  xtitle("") 	///
				 saving(FigureA4_D`d', replace) legend(off) 		
		label define t_labs 0 "Control" 1 "$600 Loan" 2 "$600 Grant" 3 "$1200 Grant" 10 "Control" 11 "$1800 Loan" 12 "$1800 Grant", modify
		/*
		graph twoway 	(bar b treatment if treatment == 11 , barwidth(.9) fcolor(`L18_color') lcolor(`L18_border') lwidth(vthin))					///
						(bar b treatment if treatment == 12 , barwidth(.9) fcolor(`G18_color') lcolor(`G18_border') lwidth(vthin))					///
						(rcap U L treatment, color(`cap_color') msize(medium) lwidth(medthin)) if inlist(decile_for_figure, `d'),			///
				ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin' `ymax') noline)							///
				title(`d`d'_title', size(medsmall) color(black))							///
				graphregion(color(white))							///
				scale(1.5) fysize(100) fxsize(70) 					///
				xlabel(11(1)12,nolabel tlcolor(none) labsize(*.6) angle(45))  xtitle("") 	///
				 saving(FDOS_D`d'_2017_ForCombined_NoControl, replace) legend(off) 	
				 */
		}

		grc1leg   							///
						"FigureA4_D3to10.gph"							///
						"FigureA4_D1.gph"							///
						"FigureA4_D2.gph"							///
						"FigureA4_D3.gph"							///
						"FigureA4_D4.gph"							///
						"FigureA4_D5.gph"							///
						"FigureA4_D6.gph"							///
						"FigureA4_D7.gph"							///
						"FigureA4_D8.gph"							///
						"FigureA4_D9.gph"							///
						"FigureA4_D10.gph"							///
						,xsize(20) ysize(7) rows(1)	ycommon	  ///
				scale(1) graphregion(color(white)) imargin(0 0 0 0)				///
				title(Decile of Expected Contribution, size(*0.8) color(black))	///
				saving(FigureA4, replace)
	
	graph combine "FigureA4", 					///
		graphregion(color(white)) imargin(0 0 0 0)	 xsize(20) ysize(10)		///
		scale(1.3)
	graph export "FigureA4.pdf", as(pdf) replace
	local files_to_remove 	FigureA4_D3to10 FigureA4_D1 FigureA4_D2 FigureA4_D3 			///
							FigureA4_D4 FigureA4_D5 FigureA4_D6			///
							FigureA4_D7 FigureA4_D8 FigureA4_D9			///
							FigureA4_D10 FigureA4
							
	foreach file in `files_to_remove' {
	rm "`file'.gph"
	}	
	gen year = 2017
	append using `datapre2017'

	label define t_labs 0 "Control" 1 "  $600 Loan" 2 "  $600 Grant" 3 "$1200 Grant" 10 "Control" 11 "$600 Loan" 12 "$600 Grant", modify
	label values treatment t_labs
	graph twoway 	(bar b treatment if treatment == 1 & year == 2016 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 2 & year == 2016 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3 & year == 2016 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(rcap U L treatment if year == 2016, color(`cap_color')  msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 1),			///
		ylabel(`ymin'(10)`ymax', labsize(*.8) ) yscale(range(`ymin' `ymax'))							///
		ytitle("Treatment Effect (pp)", size(*.8))						///				title(`d1_title', size(medsmall))							///
		graphregion(color(white)) title("2015`=uchar(8211)'2016" "Cohorts", size(small) color(black))						///
		scale(1.5) fysize(100) fxsize(120) 					///
		xlabel(1(1)3, valuelabel tlcolor(none) labsize(*.8) angle(45))  xtitle("") 	///
		 saving(Figure4_D1_2016, replace) 				///
		 legend(off order(1 "Control" 2 "$600 Loan" 3 "$600 Grant" 4 "$1200 Grant") ///
		 rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.3)  bmargin(zero) size(*1))
	graph twoway 	(bar b treatment if treatment == 1 & year == 2017 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 2 & year == 2017 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3 & year == 2017 , barwidth(.9) fcolor(`G12_color') lcolor(`G12_border') lwidth(vthin))					///
					(rcap U L treatment if year == 2017, color(`cap_color')  msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 1),			///
		ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin' `ymax') noline)							///
		title(`d`d'_title', size(medsmall) color(black))							///
		graphregion(color(white)) title("2017" "Cohort", size(small) color(black))							///
		scale(1.5) fysize(100) fxsize(90) 					///
		xlabel(1(1)3, valuelabel tlcolor(none) labsize(*.8) angle(45))  xtitle("") 	///
		 saving(Figure4_D1_2017, replace) 				///
		 legend(off order(1 "Control" 2 "$600 Loan" 3 "$600 Grant" 4 "$1200 Grant") ///
		 rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.3)  bmargin(zero) size(*1)) 		

	graph twoway 	(bar b treatment if treatment == 1 & year == 2016 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 2 & year == 2016 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3 & year == 2016 , barwidth(.9) fcolor(`G12_color') lcolor(`G12_border') lwidth(vthin))					///
					(rcap U L treatment if year == 2016, color(`cap_color')  msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 2),			///
		ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin' `ymax') lpattern(longdash))							///
		graphregion(color(white))	 title("2015`=uchar(8211)'2016" "Cohorts", size(small)  color(black))						///
		scale(1.5) fysize(100) fxsize(60) 					///
		xlabel(1(1)3, valuelabel tlcolor(none) labsize(*.8) angle(45))  xtitle("") 	///
		 saving(Figure4_D2_2016, replace) 				///
		 legend(off order(1 "Control" 2 "$600 Loan" 3 "$600 Grant" 4 "$1200 Grant") ///
		 rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.3)  bmargin(zero) size(*1)) 		
	graph twoway 	(bar b treatment if treatment == 1 & year == 2017 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 2 & year == 2017 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3 & year == 2017 , barwidth(.9) fcolor(`G12_color') lcolor(`G12_border') lwidth(vthin))					///
					(rcap U L treatment if year == 2017, color(`cap_color')  msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 2),			///
		ylabel(`ymin'(10)`ymax', nolab tlength(0) tlcolor(white)) yscale(range(`ymin' `ymax') noline)							///
		title(`d`d'_title', size(medsmall) color(black))							///
		graphregion(color(white)) title("2017" "Cohort", size(small) color(black))							///
		scale(1.5) fysize(100) fxsize(62) 					///
		xlabel(1(1)3, valuelabel tlcolor(none) labsize(*.8) angle(45))  xtitle("") 	///
		 saving(Figure4_D2_2017, replace) 				///
		 legend(off order(1 "Control" 2 "$600 Loan" 3 "$600 Grant" 4 "$1200 Grant") ///
		 rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.3)  bmargin(zero) size(*1)) 		

	graph combine  							///
						"Figure4_D1_2016.gph"							///
						"Figure4_D1_2017.gph"							///
						, rows(1) fysize(100) fxsize(100)	ycommon	///
				scale(1.3) graphregion(color(white)) imargin(0 0 0 0)				///
				saving(Figure4_D1, replace)		///
				title("1st Decile of Expected Contribution",  color(black) size(*.7))
					
	graph combine  							///
						"Figure4_D2_2016.gph"							///
						"Figure4_D2_2017.gph"							///
						, rows(1) fysize(100) fxsize(87)	ycommon	///
				scale(1.3) graphregion(color(white)) imargin(0 0 0 0)				///
				saving(Figure4_D2, replace)		///
				title("2nd Decile of Expected Contribution",  color(black) size(*.7))									
		
		
	graph combine  	`"Figure4_D1.gph"'							///
					`"Figure4_D2.gph"'							///
						, rows(1) ysize(10) xsize(18)	ycommon	///
				scale(1.3) graphregion(color(white)) imargin(0 0 0 0)				
	graph export `"Figure4.pdf"', as(pdf) replace
	
	local files_to_remove 	Figure4_D1_2016 Figure4_D1_2017 Figure4_D2_2016  			///
							Figure4_D2_2017 Figure4_D1 Figure4_D2	
							
	foreach file in `files_to_remove' {
	rm "`file'.gph"
	}	
	
	restore
}

***** FIGURE 5: Treatment Effects in 1st Decile and in 2nd–10th Deciles (2015–2017)
{
	preserve
	local ymin -10
	local ymax 20
	gen decile_for_figure = .
	gen temp_ec_dec = ec_dec
	replace ec_dec = 2 if inrange(ec_dec, 2, 10)
	drop ec_dec_*
	tab ec_dec, gen(ec_dec_)
	foreach n of numlist 1 {
		gen G6Xec_dec_`n' = got_GB6*ec_dec_`n'
		gen L6Xec_dec_`n' = got_LB6*ec_dec_`n'
		gen G12Xec_dec_`n' = got_GB12*ec_dec_`n'
		replace decile_for_figure = `n' if _n == 1+`n'
	}
	foreach n of numlist 2 {
		gen G6Xec_dec_`n' = got_GB6*ec_dec_`n'
		gen L6Xec_dec_`n' = got_LB6*ec_dec_`n'
		gen G12Xec_dec_`n' = got_GB12*ec_dec_`n'
		gen G18Xec_dec_`n' = got_GB18*ec_dec_`n'
		gen L18Xec_dec_`n' = got_LB18*ec_dec_`n'
		replace decile_for_figure = `n' if _n == 1+`n'
	}
	drop ec_dec_2
	gen G = .
	gen L = .
	gen G12 = .
	gen G18 = .
	gen L18 = .
	gen se_G = .
	gen se_L = .
	gen se_G12 = .
	gen se_G18 = .
	gen se_L18 = .

	gen treatment_recode = treatment
	tab temp_ec_dec, gen(temp_ec_dec_)
	areg FDOS G6Xec_dec_* L6Xec_dec_* G12Xec_dec_* G18Xec_dec_* L18Xec_dec_* temp_ec_dec_*, absorb(Batch) r
	foreach d of numlist 1 {
		replace G = _b[G6Xec_dec_`d'] if decile_for_figure == `d'
		replace L = _b[L6Xec_dec_`d'] if decile_for_figure == `d'
		replace G12 = _b[G12Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G = _se[G6Xec_dec_`d'] if decile_for_figure == `d'
		replace se_L = _se[L6Xec_dec_`d'] if decile_for_figure == `d'
		replace se_G12 = _se[G12Xec_dec_`d'] if decile_for_figure == `d'
	}

	
	local d = 2
	replace G = _b[G6Xec_dec_`d'] if decile_for_figure == `d'
	replace L =  _b[L6Xec_dec_`d'] if decile_for_figure == `d'
	replace G12 =  _b[G12Xec_dec_`d'] if decile_for_figure == `d'
	replace G18 = _b[G18Xec_dec_`d'] if decile_for_figure == `d'
	replace L18 = _b[L18Xec_dec_`d'] if decile_for_figure == `d'
	replace se_G = _se[G6Xec_dec_`d'] if decile_for_figure == `d'
	replace se_L =  _se[L6Xec_dec_`d'] if decile_for_figure == `d'
	replace se_G12 =  _se[G12Xec_dec_`d'] if decile_for_figure == `d'
	replace se_G18 = _se[G18Xec_dec_`d'] if decile_for_figure == `d'
	replace se_L18 = _se[L18Xec_dec_`d'] if decile_for_figure == `d'

	collapse (mean) G L G12 G18 L18 se_G se_L se_G12 se_G18 se_L18, by(decile_for_figure)
	rename (G se_G L se_L G12 se_G12 G18 se_G18 L18 se_L18) (b_1 se_1 b_2 se_2 b_3 se_3 b_4 se_4 b_5 se_5)
	reshape long b_ se_, i(decile_for_figure) j(treatment)
	rename (*_) (*)

	recode treatment (0=0) (1=2) (2=1) (3=3) (4=5) (5=4)
	label define t_lab 0 "Control" 1 "$600 Loan" 2 "$600 Grant" 3 "$1200 Grant"	4 "$1800 Loan" 5 "$1800 Grant", modify
	label values treatment t_lab

	gen L = b - se
	gen U = b + se 

	local d1_title "1st"
	local d2_title "2nd`=uchar(8211)'10th"

	tempfile temp
	save `temp'
	keep if inrange(treatment, 1, 3)
	graph twoway 	(bar b treatment if treatment == 1 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 1.00001 , barwidth(.9) fcolor(`L18_color') lcolor(`L18_border') lwidth(vthin))					///
					(bar b treatment if treatment == 2 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3 , barwidth(.9)  fcolor(`G12_color') lcolor(`G12_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3.00001 , barwidth(.9) fcolor(`G18_color') lcolor(`G18_border') lwidth(vthin))					///
					(rcap U L treatment, color(`cap_color')  msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 1),			///
			ylabel(`ymin'(10)`ymax', labsize(*.8) ) yscale(range(`ymin' `ymax'))							///
			title(`d1_title', size(medsmall) color(black)) ytitle("Treatment Effect (pp)", size(*.9))							///
			graphregion(color(white)) xscale(range(0 3))							///
			scale(1.5) fysize(100) fxsize(80) 					///
			xlabel(1(1)3,valuelabel tlcolor(none) labsize(*.8) angle(45))  xtitle("") 	///
			 saving(Figure5_D1, replace) 						///
			 legend(off order(1 "Control" 2 "$600 Loan"  3 "$1800 Loan"  4 "$600 Grant" 5 "$1200 Grant" 6 "$1800 Grant") ///
			 rows(1) region(lcolor(white)) symysize(*.6) symxsize(*.3)  bmargin(zero) size(*1)) 		

	use `temp', clear
	recode treatment (0=0) (1=1) (2=3) (3=4) (4=2) (5=5) 
	label define t_lab 0 "Control" 1 "$600 Loan" 3 "$600 Grant" 4 "$1200 Grant"	2 "$1800 Loan" 5 "$1800 Grant", modify
	graph twoway 	(bar b treatment if treatment == 1 , barwidth(.9) fcolor(`L6_color') lcolor(`L6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 2 , barwidth(.9) fcolor(`L18_color') lcolor(`L18_border') lwidth(vthin))					///
					(bar b treatment if treatment == 3 , barwidth(.9) fcolor(`G6_color') lcolor(`G6_border') lwidth(vthin))					///
					(bar b treatment if treatment == 4 , barwidth(.9)  fcolor(`G12_color') lcolor(`G12_border') lwidth(vthin))					///
					(bar b treatment if treatment == 5 , barwidth(.9) fcolor(`G18_color') lcolor(`G18_border') lwidth(vthin))					///
					(rcap U L treatment, color(`cap_color')  msize(medium) lwidth(medthin)) if inlist(decile_for_figure, 2)	///
			,ylabel(`ymin'(10)`ymax', nolab tlength(0)) yscale(range(`ymin' `ymax') noline)							///
			title(`d2_title', size(medsmall) color(black))							///
			graphregion(color(white))							///
			scale(1.5) fysize(100) fxsize(100) 					///
			xlabel(1(1)5, valuelabel tlcolor(none) labsize(*.8) angle(45))  xtitle("") 	///
			 saving(Figure5_D2to10, replace) legend(off) 		
	use `temp', clear

	graph combine  							///
					"Figure5_D1.gph"							///
					"Figure5_D2to10.gph"							///
					,xsize(20) ysize(10) rows(1)	ycommon	///
			scale(1.3) graphregion(color(white)) imargin(0 0 0 0)				///
			title(Decile of Expected Contribution, size(*.8) color(black))				///
			saving(Figure5, replace)
	
	graph combine "Figure5.gph",					///
	xsize(20) ysize(10) graphregion(color(white))
	graph export "Figure5.pdf", as(pdf) replace
	
	local files_to_remove 	Figure5_D1 Figure5_D2to10 Figure5  	
							
	foreach file in `files_to_remove' {
	rm "`file'.gph"
	}	
	restore
	
}

***** FIGURE A2: Percentage of Applicants Joining TFA in the Control Group (2015–2016)
{
	preserve
	keep if year != 2017
	tempfile temp 
	save `temp'
	collapse (mean) FDOS (semean) se_FDOS=FDOS	///
		if treatment == 0, by(ec_dec)
	tempfile byEC
	save `byEC'
	use `temp', clear
	collapse (mean) FDOS (semean) se_FDOS=FDOS	///
		if treatment == 0
	gen ec_dec = 0
	append using `byEC'
	
	gen L = FDOS - se_FDOS
	gen U = FDOS + se_FDOS 
	
	local d1_title "1st"
	local d2_title "2nd"
	local d3_title "3rd"
	local d4_title "4th"
	local d5_title "5th"
	local d6_title "6th"
	local d7_title "7th"
	local d8_title "8th"
	local d9_title "9th"
	local d10_title "10th"
	local d11_title "1st"
	local d12_title "2`=uchar(8211)'10th"

	gen one = 1
	local ymin_temp 0
	local ymax_temp 100
	graph twoway 	(bar FDOS one, barwidth(.9) fcolor(`C_color') lcolor(`C_border') lwidth(vthin))	///
					(rcap U L one, color(`cap_color') msize(medium) lwidth(medium)) if inlist(ec_dec, 0),			///
			ylabel(`ymin_temp'(20)`ymax_temp',) yscale(range(`ymin_temp' `ymax_temp') )							///
			title(All, size(medsmall) color(black)) 	ytitle("Percentage teaching on first" "day of school")						///
			graphregion(color(white))	 xscale(range(0.25 1.75))				///
			scale(1.5) fysize(100) fxsize(140)					///
			 xtitle("")				 xlabel(1(1)1,nolabel tlcolor(none))				///
			legend(off) 	///
			 saving(FigureA2_All, replace)	 

	graph twoway 	(bar FDOS one, barwidth(.9) fcolor(`C_color') lcolor(`C_border') lwidth(vthin))	///
					(rcap U L one, color(`cap_color') msize(medium) lwidth(medium)) if inlist(ec_dec, 1),			///
			ylabel(`ymin_temp'(20)`ymax_temp', nolab tlength(0) tlcolor(white)) yscale(range(`ymin_temp' `ymax_temp') lpattern(longdash))							///
			title(`d1_title', size(medsmall) color(black))							///
			graphregion(color(white)) 	 xscale(range(0.25 1.75))					///
			scale(1.5) fysize(100) fxsize(70) 					///
			xlabel(1(1)1,nolabel tlcolor(none))  xtitle("") 	///
			 saving(FigureA2_D1, replace) legend(off) 		

	foreach d of numlist 2/10 {
	graph twoway 	(bar FDOS one, barwidth(.9) fcolor(`C_color') lcolor(`C_border') lwidth(vthin))	///
					(rcap U L one, color(`cap_color') msize(medium) lwidth(medium)) if inlist(ec_dec, `d'),			///
			ylabel(`ymin_temp'(20)`ymax_temp', nolab tlength(0) tlcolor(white)) yscale(range(`ymin_temp' `ymax_temp') noline)							///
			title(`d`d'_title', size(medsmall) color(black))							///
			graphregion(color(white))	 xscale(range(0.25 1.75))						///
			scale(1.5) fysize(100) fxsize(70) 					///
			xlabel(1(1)1,nolabel tlcolor(none) labsize(*.6))  xtitle("") 	///
			 saving(FigureA2_D`d', replace) legend(off) 		
	}
		
	graph combine 	"FigureA2_All.gph"							///
					"FigureA2_D1.gph"							///
					"FigureA2_D2.gph"							///
					"FigureA2_D3.gph"							///
					"FigureA2_D4.gph"							///
					"FigureA2_D5.gph"							///
					"FigureA2_D6.gph"							///
					"FigureA2_D7.gph"							///
					"FigureA2_D8.gph"							///
					"FigureA2_D9.gph"							///
					"FigureA2_D10.gph"							///
										,						///
			graphregion(color(white)) xsize(20) ysize(8) scale(2.25)	///
			title(Decile of Expected Contribution, size(*.6) color(black)) rows(1)	///
			imargin(0 0 0 0)	
	graph export "FigureA2.pdf", as(pdf) replace
	
	local files_to_remove 	FigureA2_All FigureA2_D1 FigureA2_D2 FigureA2_D3	///
							FigureA2_D4 FigureA2_D5 FigureA2_D6 FigureA2_D7		///
							FigureA2_D8 FigureA2_D9 FigureA2_D10
							
	foreach file in `files_to_remove' {
		rm "`file'.gph"
	}	
	restore

}

***** FIGURE A3: Applicants’ Expected Contributions (2015–2017)
{
	preserve
	cap drop ec_censored
	gen ec_censored = ec*1000
	sum ec_censored if year != 2017, d
	replace ec_censored = . if ec_censored < `r(p1)'
	replace ec_censored = . if ec_censored > `r(p99)' & !missing(ec_censored)

	sum ec_censored if ec_dec1 == 1

	hist ec_censored if year != 2017, xtitle("Expected Contribution")				///
			graphregion(color(white)) percent width(250) lcolor(white) lwidth(vthin) lcolor(`L6_border') fcolor(`L6_color')	///
			addplot( pci 15 `r(max)' .1 `r(max)', lcolor(black))	///
			scale(1.5) ytitle("") legend(off)  start(-2000) ytitle(Percent) gap(10)
			
	graph export "FigureA3.pdf", as(pdf) replace
	restore	
}

***** Permutation test: NOTE that this does not run because it relies on demographics, which we cannot share.
{
if 1 == 0 {
preserve
set seed 1991
** Set this to 1 if we want to consider non-liquidity hypotheses, and to 0 otherwise
local non_liquidity_permutation_test = 0

cap log close
log using "permutation_test_log_liquidity", replace

/*************
   To start, we must mark all admits by "decile" of EC.  The quotes are because 2017 "decile"
   markers are based on the deciles from 2015--2016, as in the experiment. 
   Once we have marked decile, we can run the regressions from the paper and re-create the 
   p-values. 
**************/

// Number of deciles used in paper's analysis
local N = 10
// ec_bins holds the decile of ec for each pre-2017 obs
cap drop ec_bins
xtile ec_bins = ec if year <= 2016, n(`N')
// Use the pre-2017 decile cutoffs to label each 2017 obs by what pre-2017 
// decile it belongs to
qui sum ec
local old_max = `r(min)' - 1
foreach b of numlist 1/`N' {
	local min = `old_max'
	qui sum ec if ec_bins == `b'
	local max = `r(max)'
	replace ec_bins = `b' if ec > `min' & ec <= `max'
	local old_max = `max'
}
replace ec_bins = `N' if ec > `max' & !missing(ec)

/**************
Now we run the regressions from the paper to get the appropriate p-values.
**************/

// Race and Gender are sometimes coded -1, but factor variables can't deal with -1.  
// Giving this -1 category a new number won't change results, but enables us to dispense 
// with the annoyance that comes with xi and constructing our own interactions.
replace Race   = 99 if Race  ==-1
replace Gender = 99 if Gender==-1

// Get p-values for whole study.
areg FDOS i.ec_bins#c.LB_amount i.ec_bins#c.GB_amount  ib10.ec_bins  $demos, ///
	absorb(Batch) r

foreach T in G L {
	test 1.ec_bins#c.`T'B_amount = 0
	local exp_p_`T'_d1 = `r(p)'
}

test (1.ec_bins#c.GB_amount = 0) (1.ec_bins#c.LB_amount = 0)

// Save values actually used in the experiment
foreach var of varlist treatment GB_amount LB_amount AB_amount {
	rename `var' `var'_exp 
}

//qui 
{

// Marker for deciles 1 and 2 --- for use when we consider who an admit was randomized with.
gen ec_dec_12 = ec_dec<=2

// This is the main "permute treatment markers and look for significance" loop.
local i              = 0
local num_successes  = 0
local num_iterations = 50 //000 
while (`i ' < `num_iterations') {
	local i = `i' + 1
	noi di "********************* `i':"
	// Display progress every 50 iterations.
	//if (floor(`i'/50) == `i'/50) {
	//	noisily display "i = `i', `num_first_stage' first stage successes, `num_successes' replication successes so far"
	//} 

	// Drop variables that we will reassign 
	// (the extra rigamarole is to prevent drop from dropping, for instance, "treatment_exp" 
	// when you say "treatment" because the latter is an unambiguous abbreviation of the former.)
	foreach var in treatment GB_amount LB_amount AB_amount rand1 rand2 order1 order2 {
		capture confirm var `var', exact
		if (_rc==0) {
			drop `var'
		}
	}

	/*****
	  This is the part where we permute treatment markers.
	*****/

	// Group by Batch and ec_dec, and pick two random orderings. Note that the parenthetical 
	// on bysort means sort by that variable, but don't group based on it.
	gen rand1 = runiform(0,1)
	bysort Batch           (rand1):  gen     order1 = _n if year<=2016
	bysort Batch ec_dec_12 (rand1):  replace order1 = _n if year==2017
	gen rand2 = runiform(0,1)
	bysort Batch           (rand2):  gen     order2 = _n if year<=2016
	bysort Batch ec_dec_12 (rand2):  replace order2 = _n if year==2017
	// Sort by one ordering and permute treatment using the other.
	bysort Batch           (order1): gen     treatment = treatment_exp[order2] if year<=2016
	bysort Batch ec_dec_12 (order1): replace treatment = treatment_exp[order2] if year==2017
	//tab treatment,     nolabel
	//tab treatment_exp, nolabel
	//l Batch ec_dec_12 order1 order2 treatment treatment_exp if year==2017, nolabel

	// Assign loan and grant bumps based on treatment/EC decile/cohort
	gen     GB_amount = 0
	gen     LB_amount = 0

	replace GB_amount = GB_amount + 6*(treatment == 2) + 12*(treatment == 4) 		if year <= 2016 | ec_dec <= 2
	replace LB_amount = LB_amount + 6*(treatment == 3)  							if year <= 2016 | ec_dec <= 2

	replace GB_amount = GB_amount + 18*(treatment == 2)  						 	if year == 2017 & ec_dec > 2
	replace LB_amount = LB_amount + 18*(treatment == 3)  							if year == 2017 & ec_dec > 2
	gen     AB_amount = LB_amount + GB_amount

	areg FDOS i.ec_bins#c.LB_amount i.ec_bins#c.GB_amount  ib10.ec_bins  $demos, ///
		absorb(Batch) r

	forvalues D = 1/10 {
		foreach T in G L {
			test `D'.ec_bins#c.`T'B_amount = 0
			local perm_p_`T'_d`D'= `r(p)'
		}
		if (min(`perm_p_G_d`D'',`perm_p_L_d`D'')<=min(`exp_p_G_d1',`exp_p_L_d1')) {
		     if (max(`perm_p_G_d`D'',`perm_p_L_d`D'')<=max(`exp_p_G_d1',`exp_p_L_d1')) {
		     	local num_successes = `num_successes' + 1
		     	break
		     }
		}
	}
	noi di "`num_successes'"
}
}
noi di "Successes: `num_successes'"
noi di "Iterations: `num_iterations'"
noi di "p-value:`=`num_successes'/`num_iterations''"


	
	local replication = 0
	local success_N = 0
	estimates clear
	// This loop cycles through whether we are looking at the bottom decile, quintile, etc.
	foreach N of numlist 1 2 3 4 5 10 {
		local rep_type = 0
		local replication = 0
		// If we've already found a successful replication, move on 
		if (`success' == 1) continue
		*noisily display "i = `i', N = `N'"
		** Cut the pre-2017 data into N bins by EC
		cap drop ec_bins
		if `N' > 1 {
			xtile ec_bins = ec if year <= 2016, n(`N')
			** Now we need to use the same EC thresholds to assign bins in 2017
			qui sum ec
			local old_max = `r(min)' - 1
			foreach b of numlist 1/`N' {
				local min = `old_max'
				qui sum ec if ec_bins == `b'
				local max = `r(max)'
				replace ec_bins = `b' if ec > `min' & ec <= `max'
				local old_max = `max'
			}
			replace ec_bins = `N' if ec > `max' & !missing(ec)
		}
		if `N' == 1 gen ec_bins = 1
		** Make dummy variables for each bin and interat them with treatment
		cap drop *ec_bins_*
		tab ec_bins, gen(ec_bins_)
		foreach b of numlist 1/`N' {
			foreach T in G L {
				gen `T'B_amountXec_bins_`b' = `T'B_amount*ec_bins_`b'
			}
		}
		** Get p-values for 2015-6
		eststo: xi:	areg FDOS LB_amountXec_bins_* GB_amountXec_bins_*  ec_bins_*  $demos if year < 2017, absorb(Batch) r
		foreach T in G L {
			qui test `T'B_amountXec_bins_1 = 0
			local orig_p_`T' = `r(p)'
		}
		** If both p-values are low enough, we'll do a replication
		if ((`orig_p_G' <= `real_orig_p_G' & `orig_p_L' <= `real_orig_p_L') 		///
		|  (`orig_p_L' <= `real_orig_p_G' & `orig_p_G' <= `real_orig_p_L')) {
			** Both positive
			if sign(_b[GB_amountXec_bins_1]) > 0 & sign(_b[LB_amountXec_bins_1]) > 0 {
				local replication = 1
				local rep_type = 1
			}
			** Both negative
			if sign(_b[GB_amountXec_bins_1]) < 0 & sign(_b[LB_amountXec_bins_1]) < 0 {
				local replication = 1
				local rep_type = 2
			}
			** Grants positive, Loans negative
			if sign(_b[GB_amountXec_bins_1]) > 0 & sign(_b[LB_amountXec_bins_1]) < 0 {
				local replication = 1
				local rep_type = 3
			}
		}
		** If just loans are significant, we'll do a replication, 
		** but we only allow effect of loans to be negative and grants zero,
		** not loans positive and grants zero.
		if (`orig_p_L' <= `real_orig_p_G' & `orig_p_L' <= `real_orig_p_L') 		///
		&  (`orig_p_G' > `real_orig_p_G' & `orig_p_G' > `real_orig_p_L') 	///
		&  _b[LB_amountXec_bins_1] < 0 {
			local replication = 1
			local rep_type = 4
		}
		** If just grants are significant, we'll do a replication, 
		** but we only allow effect of grants to be positive and loans zero,
		** not grants negative and loans zero
		if (`orig_p_G' <= `real_orig_p_G' & `orig_p_G' <= `real_orig_p_L') 		///
		&  (`orig_p_L' > `real_orig_p_G' & `orig_p_L' > `real_orig_p_L') 	///
		& sign(_b[GB_amountXec_bins_1]) > 0 {
			local replication = 1
			local rep_type = 5
		}

		local success_type = 0
		** If we're only considering liquidity, only allow rep_type = 1
		if `non_liquidity_permutation_test' == 0 & `rep_type' != 1 {
			local replication = 0
		}
		if `replication' == 1 {	
			** Add to the first stage counter only if this is first replication of permutation
			if `first_stage' == 0 local num_first_stage = `num_first_stage' + 1
			local first_stage = 1
			** Get p-values from the replication
			eststo: xi:	areg FDOS LB_amountXec_bins_* GB_amountXec_bins_*  ec_bins_*  $demos if year == 2017, absorb(Batch) r
			foreach T in G L {
				qui test `T'B_amountXec_bins_1 = 0
				local rep_p_`T' = `r(p)'
			}
			if `rep_type' == 1 {
				** Counts as success if both p-values are low enough AND both signs are positive
				if ((`rep_p_G' <= `real_rep_p_G' & `rep_p_L' <= `real_rep_p_L') 		///
				|  (`rep_p_L' <= `real_rep_p_G' & `rep_p_G' <= `real_rep_p_L')) 	///
				& sign(_b[GB_amountXec_bins_1])> 0 & sign(_b[LB_amountXec_bins_1])  > 0	{
					local success = 1
					local success_type = 1
				}
			}
			if `rep_type' == 2 {
				** Counts as success if both p-values are low enough AND both signs are negative
				if ((`rep_p_G' <= `real_rep_p_G' & `rep_p_L' <= `real_rep_p_L') 		///
				|  (`rep_p_L' <= `real_rep_p_G' & `rep_p_G' <= `real_rep_p_L')) 	///
				& sign(_b[GB_amountXec_bins_1])< 0 & sign(_b[LB_amountXec_bins_1])  < 0	{
					local success = 1
					local success_type = 2
				}
			}
			if `rep_type' == 3 {
				** Counts as success if both p-values are low enough AND grant effect positive AND loan effect negative
				if ((`rep_p_G' <= `real_rep_p_G' & `rep_p_L' <= `real_rep_p_L') 		///
				|  (`rep_p_L' <= `real_rep_p_G' & `rep_p_G' <= `real_rep_p_L')) 	///
				& sign(_b[GB_amountXec_bins_1])> 0 & sign(_b[LB_amountXec_bins_1])  < 0	{
					local success = 1
					local success_type = 3
				}
			}
			if `rep_type' == 4 {
				** Counts as success if loans p-value is low enough and grants p-value is large enough,
				** and effect of loans is negative
				if (`rep_p_L' <= `real_rep_p_G' & `rep_p_L' <= `real_rep_p_L') 		///
				&  (`rep_p_G' > `real_rep_p_G' & `rep_p_G' > `real_rep_p_L') 	///
				&  _b[LB_amountXec_bins_1] < 0 {
					local success = 1
					local success_type = 4
				}
			}
			if `rep_type' == 5 {
				** Counts as success if grants p-value is low enough and loans p-value is large enough,
				** and effect of grants is positive
				if (`rep_p_G' <= `real_rep_p_G' & `rep_p_G' <= `real_rep_p_L') 		///
				&  (`rep_p_L' > `real_rep_p_G' & `rep_p_L' > `real_rep_p_L') 	///
				&  _b[GB_amountXec_bins_1] > 0 {
					local success = 1
					local success_type = 5
				}
			}
		}
		** Count up the number of successes
		local num_successes = `num_successes' + `success'
		if `success' == 1 local success_N = `N'
	}
	** Show what's going on under the hood
	if (`success' == 1 | `success' == 0)  {
		noisily esttab, keep(?B_amountXec_bins_*) p 						///
		title(success = `success', success type = `success_type', N = `success_N') ///
		compress
	}

display "First stage successes = `num_first_stage', second stage successes = `num_successes'"
display "proportion successes = " `num_successes'/`num_iterations'
restore

log close
}
}




